Problem/Motivation
Running phpcs after installing coder (via composer). Seeing many errors like this:
567 | ERROR | Type hint "array" missing for $element
567 | ERROR | Type hint "array" missing for $form_state
for function declarations like this one
/**
* Form validation function to see if product id already exists.
*
* @param array $element
* Form element to validate.
* @param array $form_state
* Form state object.
*/
function bv_form_product_id_validate($element, $form_state) {
...
}
Type hinting is not, as far as I can tell, a current Drupal coding standard for function declarations (see https://www.drupal.org/coding-standards#functdecl), so these errors seem to be incorrect - running phpcs --standard=Drupal should not flag them.
These errors have been replicated on two different systems - Mac OS X Yosemite and Ubuntu 10.0.4
Mac:
DevelopmentsMBP:blink_jjbos jeff$ phpcs --version;php --version;composer global info --installed
PHP_CodeSniffer version 2.3.3 (stable) by Squiz (http://www.squiz.net)
PHP 5.5.27 (cli) (built: Jul 23 2015 00:21:59)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
Changed current directory to /Users/jeff/.composer
drupal/coder 8.2.3 Coder is a library to review D...
squizlabs/php_codesniffer 2.3.3 PHP_CodeSniffer tokenizes PHP,...
Ubuntu:
jeffmarkel@swizzle:~$ phpcs --version;php --version;composer global info --installed
PHP_CodeSniffer version 2.3.3 (stable) by Squiz (http://www.squiz.net)
PHP 5.3.2-1ubuntu4 with Suhosin-Patch (cli) (built: Apr 9 2010 08:23:39)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans
Changed current directory to /home/jeffmarkel/.composer
drupal/coder 8.2.3 Coder is a library to review Drupal code.
squizlabs/php_codesniffer 2.3.3 PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined se...
Proposed resolution
Remove code which tests for type hints and subsequently returns false positives.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
jmarkel commentedHere's a patch which removes the section of code that checks for and returns the false positives 'Type hint' errors.
Comment #3
jmarkel commentedComment #5
klausiI committed a fix that does not throw this error when arrays are used. For classed objects we have in our object oriented coding standards:
https://www.drupal.org/node/608152#hinting
So using type hints in general is encouraged and I think we should keep this sniff that if you document a parameter as a class/interface you should also use that class/interface as type hint.
Comment #6
pfrenssenThis is a de facto standard. I have seen many reviews for D8 core issues in which it was pointed out that a type hint for an array was missing. I just did a grep of core and there are 5500+ instances where this type hint is used.
Even more important, if your argument is declared as an array and the type hint is not there then you are missing out on a very helpful mechanism in PHP that will help you discover bugs. If you expect an array and get a different type then you should be warned about this. You could even argue that omitting this type hint is a bug in itself.
I cannot say how many times this sniff has helped me finding bugs in my code and I'm sad to see that it has been removed. Can we revert this, or at least have it throw a warning instead of an error?
I will also open an issue to have this formally added to the coding standards.
Comment #7
pfrenssenThere is already an issue for adding array type hints to the coding standards: #1158720: Improve text for parameter type hinting in function declaration. In that issue @YesCT, @jhodgdon and @chx (by proxy) agree on adding it, and nobody is against. It seems it is just a matter of actually writing the documentation.
Quoting from that issue:
Reopening this for discussion.
Comment #8
jmarkel commentedRe #6 and #7, I agree that this should be considered as an addition to the standard. it's been floating around as a proposal for at least 4 years, but to date the proposal is no more than that - a proposal. But the rules embodied in Coder are, or should be, a reflection of the standards that are, not the ones that should be, so the discussion should be taking place in #1158720: Improve text for parameter type hinting in function declaration. If consensus is reached there and the standards are changed, that's when this sniff should be re-added.
Comment #9
klausiAgreed, let's revisit this once the standard has been changed.
Comment #11
klausiThis has now been reverted in #2803251: Restore the rule to require array type hints because we will require type hints in the coding standards.
Comment #12
greg boggsThis error is back, I think? I have a type hint for an array and getting this error again:
79 | ERROR | Type hint "array" missing for $array
Comment #13
klausiDo you have "array" as type hint on the function signature? Or is it missing there?
Comment #14
admirernepali commentedThe error is happening for me too: