Note this only applies to Fivestar in Drupal 7. For Drupal 8, see #3133101: [D8] Coding standards

Fivestar 7.x-2.x was written under a previous set of coding standards - Drupal core has changed standards drastically since then, with many new standards aimed primarily at D8 code. Drupal core policy is this:

All new code should follow the current standards, regardless of (core) version. Existing code in older versions may be updated, but doesn't necessarily have to be. Especially for larger code-bases (like Drupal core), updating the code of a previous version for the current standards may be too huge of a task. However, code in current versions should follow the current standards.

There are more reasons than just "may be too huge of a task" to leave the 7.x-2.x code mostly untouched - modifying class names, variable names, etc. has a great potential to break code and break our integration with modules like Views. Likewise, coding standards patches touch many parts of the code base, so have a great potential to break other work-in-progress patches that address real problems (bug reports) or make real improvements (feature requests) - coding standards changes are extremely minor in importance related to these.

Because of this, I am not very interested in putting in a lot of effort to change anything EXCEPT for things that may / are known to cause problems (like extra whitespace or missing EOL at ends of files) and things that will actually HELP people who use Fivestar (like missing or incomplete documentation comments).

If you would like to submit patches here, make sure they are restricted to one and only one type of standards violation - that will make them easier to review. We don't need multiple issues for this - all coding standards patches should be posted in this issue.

Issue fork fivestar-3137082

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TR created an issue. See original summary.

TR’s picture

Line length.

  • TR committed a704b97 on 7.x-2.x
    Issue #3137082 by TR: [D7] Coding standards - line length.
    
urvashi_vora’s picture

Assigned: Unassigned » urvashi_vora
urvashi_vora’s picture

Assigned: urvashi_vora » Unassigned
Status: Active » Needs review
FileSize
51.77 KB

Hi,

Please review this patch.

Thanks

Status: Needs review » Needs work

The last submitted patch, 5: coding-standard-resolved-3137082-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

TR’s picture

Thank you for wanting to help, but coding standards changes can be tricky because phpcs reports a lot of false problems and because making coding standards changes in the code can't improve functionality, but CAN break things. Because of this, you can't simply change the code just to make the errors go away - you have to understand first what phpcs is complaining about and why, and make an informed decision whether something should be changed or not, and decide HOW it should be changed. Not every reported violation should be fixed.

As I said in the issue summary:

If you would like to submit patches here, make sure they are restricted to one and only one type of standards violation - that will make them easier to review.

But this patch tries to address too many things, and it would take to long to point out every single problem then re-review the patch when you fixed all those problems. Please try to fix only one type of standards violation - you can submit other patches after that to fix other types.

Right at the start of the patch there's a problem - you modify fivestar.api.php to remove coding standards messages similar to "Hook implementations should not duplicate @return documentation". This is a false positive. The fivestar.api.php file is where all hooks are documented. This IS the primary hook documentation, this is NOT duplicate documentation. If you remove this documentation in the api.php file, then the Fivestar hooks won't have any documentation.

Also, right at the end of the patch there's a problem - you do this:

   /**
    * A user with permission to vote.
+   *
+   * @var voterUser
+   * The voter user.
    */
   protected $voterUser;
 
@@ -33,9 +42,14 @@ class FivestarBaseTestCase extends AJAXTestCase {
   public function setUp() {
     parent::setUp(array('fivestar', 'dblog'));
 
-    $type = $this->drupalCreateContentType(array('type' => 'test_node_type', 'name' => 'test_node_type'));
-    $this->adminUser = $this->drupalCreateUser(array('create test_node_type content', 'rate content'));
-    $this->voterUser = $this->drupalCreateUser(array('rate content'));
+    // $type = $this->drupalCreateContentType(array('type' => 'test_node_type',

Three things wrong with this:

  1. Yes, the properties need to be documented with @var tags, but the @var should document the datatype of the variable, it should not be the name of the variable.
  2. You add an additional unneeded comment after the @var. There should be nothing after the @var.
  3. You comment out code. That should never be done, and in this case you are causing the test to fail because you commented out the creation of the content type which the test tries to use in the next two lines.

There are many other problems with the patch - like adding @inheritdoc to functions that don't/can't inherit documentation, Instead you should be writing actual documentation.

Also, when you do something like

-    'titleUser' => t('Your rating') . ': ',
-    'titleAverage' => t('Average') . ': ',
+    'titleUser' => t('Your rating:'),
+    'titleAverage' => t('Average:'),

you make the coding standards message disappear, but you also BREAK all the translations for this module because you've removed the old strings from the translation table and added new strings which don't have translations. That is not something that should be changed, especially since the D7 version of this module has been around for so long and D7 is close to obsolescence.

Please submit a new patch that only tries to address one type of coding standards message.

urvashi_vora’s picture

Hi @TR,

Thanks for your valuable feedback. I will provide another patch.

urvashi_vora’s picture

Assigned: Unassigned » urvashi_vora
urvashi_vora’s picture

Hi @TR,

Here is the patch for "The array declaration extends to column 128 (the limit is 80). The array content should be split up over multiple lines" errors.

Please review this patch.

Thanks

urvashi_vora’s picture

Hi @TR,

Here is the patch for the "Line exceeds 80 characters" error.

Please review the patch.

Thanks

urvashi_vora’s picture

Assigned: urvashi_vora » Unassigned
Status: Needs work » Needs review
FileSize
16.56 KB
3.65 KB

Hi @TR,

Here is the patch for "Doc comment is empty"

Please review. Thanks

urvashi_vora’s picture

Hi @TR,

Here is the patch for "Missing @var tag in member variable comment" and "Missing parameter type".

Please review this patch.

Thanks

erikaagp’s picture

Status: Needs review » Reviewed & tested by the community
apaderno’s picture

Title: [D7] Coding standards » Coding standards
Status: Reviewed & tested by the community » Needs work
   /**
-   *
+   * Constructor function.
    */
   public function __construct() {
     $this->registerTypes(array('fivestar'));
   }

That is not how a class constructor is documented.

   /**
-   *
+   * Function fields() implementation.
    */
   public function fields($type, $parent_field, $migration = NULL) {

A method that extends a parent method or that implements an interface method is documented with {@inheritdoc}.

 /**
- *
+ * Implementing function fivestar_get_votes().
  */
 function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) {
   $multiple = fivestar_get_votes_multiple($entity_type, array($id), $tag, $uid);

That is not what a function documentation comment should say. It misses the parameters declaration too.

nitin_lama’s picture

Assigned: Unassigned » nitin_lama
nitin_lama’s picture

Assigned: nitin_lama » Unassigned
Status: Needs work » Needs review
FileSize
38.54 KB
20.12 KB

Fixed remaining coding standards and addressed #15

apaderno’s picture

Status: Needs review » Needs work
- * Converts all existing fivestar/node_type settings into fields with exposed fivestar formatters.
+ * Converts all existing fivestar/node_type settings into fields.
+ *
+ * Along with exposed fivestar formatters.

The short description needs to be 80 characters or less; that change is correct. The long description needs to be a sentence too, while Along with exposed fivestar formatters is a phrase. The long description can repeat what the short description already said.

  /**
-   *
+   * Constructor, initialize registerTypes.
    */

That still isn't how constructors are documented.

   /**
-   *
+   * Implementing function prepare().
    */

That isn't how methods are documented.

-    return isset($return) ? $return : NULL;
+    return $return ?? NULL;

That change isn't required from the Drupal coding standards. Drupal 7 doesn't require PHP 7 as minimum PHP version; the null coalescing operator cannot be used.

 /**
- *
+ * Internal function _fivestar_variables() implementation.
  */

That documentation comment isn't correct: It doesn't add any information that isn't already given from the code, nor does it explain what the purpose of the function is.
While there are similar comments for other functions, adding that documentation comment where there is no documentation comment is useless. Eventually, the other documentation comments should be changed.

 /**
+ * Implementing function fivestar_get_votes().
  *

The short description for a function must describe what that function does or, in the case of hook implementations, which hook the function implements.

-      'name' => t($tag),
+      'name' => $tag,

While this change could be correct, it's not a change required from Drupal coding standards.

+ *   Returns TRUE if a user can vote.
+ *   Returns FALSE if a user cannot vote.

That isn't a necessary change, nor a change that Drupal coding standards require.

-    list($entity_type, $field_name) = explode(':', $subtype['subtype_id'], 2);
+    [$entity_type, $field_name] = explode(':', $subtype['subtype_id'], 2);

list() isn't a deprecated function, and the coding standards don't require that change.

 /**
- * Implements hook_ctools_content_subtype_alter()
+ * Implements hook_alter().
  */

The existing comment only needs a period.

 /**
- *
+ * Implementing function fivestar_form_field_ui_field_edit_form_alter().
  */
 function fivestar_form_field_ui_field_edit_form_alter(&$form, $form_state) {

That is not how a hook is documented.

 /**
- *
+ * Implements class for view handler filter in operator.
  */

A class short description should say what the class purpose is.

   /**
    * A user with permission to administer Fivestar.
+   *
+   * @var user
    */
   protected $adminUser;
 
   /**
    * A user with permission to vote.
+   *
+   * @var user
    */
   protected $voterUser;

user is not a correct datatype. The Drupal coding standards say what can be used after @var and that isn't an allowed value.

nitin_lama’s picture

Assigned: Unassigned » nitin_lama
nitin_lama’s picture

rerolled patch #17 and addressed #15 & #18 except constructor documentation.

nitin_lama’s picture

Assigned: nitin_lama » Unassigned
apaderno’s picture

What reported in comments #15 and #18 hasn't been yet addressed.

kartiktandon’s picture

Assigned: Unassigned » kartiktandon

I am Working on it

TR’s picture

Well I don't know what to do with this - it keeps getting worse.
The latest MR has all sorts of new problems. It looks to me like it was forked from a version of Fivestar that is over 2.5 YEARS out of date. For example:

diff --git a/widgets/basic/basic-rtl.css b/widgets/basic/basic-rtl.css
old mode 100755
new mode 100644
index 7c802055b217ac652df0662cd337670ee9054e7d..7205a5dee3537088e30c4f51b616b76ae9c526ac
--- a/widgets/basic/basic-rtl.css
+++ b/widgets/basic/basic-rtl.css
@@ -1,10 +1,11 @@
 /* Adjust the background position when using this widget with a RTL langauge. */
-div.fivestar-widget-static .star span.on {
+.fivestar-basic div.fivestar-widget-static .star span.on {
   background-position: right -32px;
 }
-div.fivestar-widget div.on a {
+.fivestar-basic div.fivestar-widget div.on a {
   background-position: right -16px;
 }
-div.fivestar-widget div.hover a, div.rating div a:hover {
+.fivestar-basic div.fivestar-widget div.hover a,
+.fivestar-basic div.rating div a:hover {
   background-position: right -32px;
-}
\ No newline at end of file
+}

The permissions on that file were fixed (by me) by commit d9b735be8d6ac4038 on 3 May 2020.

Likewise, as far as I can tell ALL the changes to the CSS files are out of scope of this issue - those changes are NOT coding standards changes, they change the actual properties and selectors. These changes have not been discussed and do not belong here.

While improvements to the advanced help are appreciated, again those go far beyond a "coding standards" change. And they should be done in the CURRENT version of the module. The current version of the module needs to be fixed first, then those changes can be backported to the amost-obsolete D7 version. Fixing it in D7 first is a good way to waste effort, because that version (along with the improvements) will disappear soon.

There's also a lot of D8 code in there. For example the new file fivestar.php which has class Fivestar extends FormElement { - this is not Drupal 7 code.

kartiktandon’s picture

kartiktandon’s picture

Assigned: kartiktandon » Unassigned
kartiktandon’s picture

Status: Needs work » Needs review
TR’s picture

Status: Needs review » Needs work

Patch does not apply and patch introduces new coding standards problems (trailing whitespace).
Please post an interdiff with any patch so we can see what you changed.

Short array syntax can't be used in D7 because it doesn't work with PHP 5.3. Drupal 7 still supports PHP 5.3.

TR’s picture

@apaderno: As you said in #18:

list() isn't a deprecated function, and the coding standards don't require that change.

and

-    return isset($return) ? $return : NULL;
+    return $return ?? NULL;

That change isn't required from the Drupal coding standards. Drupal 7 doesn't require PHP 7 as minimum PHP version; the null coalescing operator cannot be used.

Agreed, HOWEVER the results from PHPCS run by DrupalCI report these as errors. See for example https://www.drupal.org/pift-ci-job/2502899
DrupalCI is using sniffs that apply standards that haven't been adopted by Drupal and aren't enforced in Drupal core. This is an ongoing problem for contributed modules.

I consider those false positives, which should not be "fixed". As I said in #7 , when creating coding standards patches one has to "make an informed decision whether something should be changed or not, and decide HOW it should be changed. Not every reported violation should be fixed."

lalitkyttn’s picture

Assigned: Unassigned » lalitkyttn

I am working on this

TR’s picture

@lalitkyttn: Still working on it?

Amit.Rawat777’s picture

Assigned: lalitkyttn » Unassigned
Status: Needs work » Needs review
FileSize
10.29 KB

fixed some coding standard issues.

apaderno’s picture

Status: Needs review » Needs work
 /**
+ * Provides a preview of a Five Star widget.
+ *
+ * @return string
+ *   The rendered HTML output for the preview.

The module name is Fivestar.

       '#default_value' => $this->getSetting('rated_while'),
       '#title' => $this->t('Select when user can rate the field'),
       '#options' => [
-        'viewing' => 'Rated while viewing',
-        'editing' => 'Rated while editing',
+        'viewing' => $this->t('Rated while viewing'),
+        'editing' => $this->t('Rated while editing'),

The issue has been created for the 7.x-1.x branch, which does not contain that code. That is code from the Drupal 8 branch (and this explains why the patch fails to apply).

TR’s picture

Title: Coding standards » [D7] Coding standards

Yashaswi18 made their first commit to this issue’s fork.

Shreyas gowda’s picture

FileSize
9.03 KB
apaderno’s picture

MR!12 has merge conflicts that must be manually resolved.

zkhan.aamir’s picture

Issue tags: +Coding standards