Closed
Description
Hi @Rarst, as discussed on Slack, please find some initial feedback below:
Note: all current feedback is just and only based on a quick look at the codebase. I haven't run any tests with the repo (yet).
composer.json
:
- Why minimum PHP 7.2 ? Not needed for PHPCS (minimum PHP 5.4) and you're severely limiting the potential public which can use the sniff as people often don't want
dev
dependencies with a higher minimum PHP version as their own project (and Composer warns against doing that). - Please remove the
autoload
section. This is known to cause issues with PHPCS. - PHP_CodeSniffer should be a
no-dev
requirement, not adev
requirement. Otherwise users may still get an older PHPCS version if they also use other external standards which have a lower minimum PHPCS version.
.gitignore
:
- Please ignore your tests.
Readme:
- Composer install: You may want to suggest for people to use
require --dev
as this sniff is normally not a production requirement for other packages. - Standalone install:
- missing instructions on installing PHPCS
- I'd recommend telling people to set the
installed_paths
and use the ruleset by name. There are numerous bugs known for when standards are not installed.
Ruleset:
- You shouldn't need the
<autoload>
in the ruleset. PHPCS >= 3.1.0 should handle this fine as is. Though maybe not if the standard is not installed... hmm.... well, see my remark above in that case. - Don't set properties for your own sniff in the ruleset. Set them as the default property value for the sniff instead (which you already have done).
And you don't need to include the sniff in the ruleset, all sniffs in theSniffs
directory of a standard will automatically be included. - Please add the XSD to the ruleset.
- You should also set the namespace you are using in the ruleset as it doesn't follow the base pattern expected by PHPCS, so you're breaking the autoloader.
Have a look at WPCS to see an example.
Sniff code:
- See above my remark about the namespace. You can use this, but not without setting the namespace in the ruleset. You are currently breaking the build in autoloader from PHPCS.
- Minor nitpick: you are using non-standard names for the parameters received for
process()
making the cognitive load for other sniff devs higher (breaks the principle of least astonishment). $method
: you are presuming a code style. Best practice sniffs should be code style agnostic.
While not very common, this will break on:public function // some comment functionName() {}
- The PHPCS
addError()
function already hassprintf()
build in, so no need to do it yourself. Just pass the replacements as an array in the fourth parameter. - The errorcode should be descriptive and not contain characters which may break in XML. Using
self::class
is a bad idea as:- It repeats the sniff name (useless).
- Is non-descriptive
- May break XML.
- Makes the sniff error code unnecessarily long.
I'd suggest using a plain string likeTooHigh
.
You could even consider setting two levels, likemaxCognitiveComplexity
andrecommendedCognitiveComplexity
, throw an error for the first, a warning for the second and have separate error code for each, likeTooHigh
andHigh
Analyzer code:
$booleanOperators
- you are missing three operators. Please consider using the PHPCS nativeTokens::$booleanOperators
array instead.$operatorChainBreaks
- what about null coalesce ?- For performance reasons, set the token arrays with the token constants as keys and use
isset()
instead ofin_array()
. - Again for performance reasons, do a quick check against which tokens you want to handle at the start of the main loop in
computeFor...()
andcontinue
if not one of your targets.
You are now also passing whitespace and comment tokens off for processing which is just plain inefficient. $nextToken
inisIncrementingToken()
is not code style agnostic.
Tests:
- These are definitely still using the wrong (copied) namespace.
Anyways, hope this helps.
Metadata
Metadata
Assignees
Labels
No labels