php-fig-rectified / psr2r-sniffer Goto Github PK
View Code? Open in Web Editor NEWA PSR-2-R code sniffer and code-style auto-correction-tool - including many useful additions
License: MIT License
A PSR-2-R code sniffer and code-style auto-correction-tool - including many useful additions
License: MIT License
Lets say that I have the following
<?php
class ClassA {
public function __construct(PimpleContainer $container){
/** \MyNamespace\ClassB $classB */
$classB = $container['class_b'];
}
}
If I understood correctly the above is not allowed by the NoInlineFullyQualifiedClassName
But also the below is not allowed by the UnusedUseStatement
<?php
use \MyNamespace\ClassB;
class ClassA {
public function __construct(PimpleContainer $container){
/** ClassB $classB */
$classB = $container['class_b'];
}
}
@dereuromark - I'm considering documenting how this psr2r-sniffer works (internals documentation) in support of bringing people into one aspect of php internals (lexing/tokenizing; examining how PHP source is structured) - https://gist.github.com/ewbarnard/97ba2259f0e05e4331bbaa3394da4084
Of course getting people to think about the POINT of this project is a plus. That's why I'd like to use this as a route to understanding how the Code Sniffer works, and how the sniffs consume tokens.
I was wondering if there is any chance that support would be added for PHPStan generics (https://phpstan.org/blog/generics-by-examples). They are very useful for getting precise type validation from static analysis, but the PSR-2-R sniffs aren't happy with them.
The following is an example of a doc block for a function that takes a class string and constructor arguments, and outputs an instance of the class.
/**
* @template T of object
* @param class-string<T> $name The name of the class to construct
* @param mixed ...$args The arguments to pass to the constructor
* @return T
*/
And here are the resulting PSR-2-R violations.
>> 35: » ·*·@template·T·of·object
36: » ·*·@param·class-string<T>·$name·The·name·of·the·class·to·construct
37: » ·*·@param·mixed·...$args·The·arguments·to·pass·to·the·constructor
----------------------------------------------------------------------------------------------------------------------------------------
LINE 36: ERROR [ ] Invalid typehint `class-string<T>` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 36: ERROR [x] Possible doc block error: `class-string<T> $name The name of the class to construct` seems to be missing type
`string`. (Spryker.Commenting.DocBlockParamAllowDefaultValue.Typehint)
LINE 36: ERROR [ ] Invalid typehint `class-string<T>` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 36: ERROR [ ] Invalid typehint `class-string<T>` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
----------------------------------------------------------------------------------------------------------------------------------------
34: » ·*
35: » ·*·@template·T·of·object
>> 36: » ·*·@param·class-string<T>·$name·The·name·of·the·class·to·construct
37: » ·*·@param·mixed·...$args·The·arguments·to·pass·to·the·constructor
38: » ·*·@return·T
----------------------------------------------------------------------------------------------------------------------------------------
LINE 38: ERROR [ ] Invalid typehint `T` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 38: ERROR [ ] Invalid typehint `T` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
LINE 38: ERROR [ ] Invalid typehint `T` (PSR2R.Commenting.FullyQualifiedClassNameInDocBlock.ClassNameInvalid)
----------------------------------------------------------------------------------------------------------------------------------------
36: » ·*·@param·class-string<T>·$name·The·name·of·the·class·to·construct
37: » ·*·@param·mixed·...$args·The·arguments·to·pass·to·the·constructor
>> 38: » ·*·@return·T
39: » ·*/
I can certainly just skip these sniffs, but I thought I'd reach out in case there was any interest in supporting this feature. Thanks!
The following passes but should not:
AppPath::get( 'View/Cell' );
//expected
AppPath::get('View/Cell');
$folderContent = ( new Folder( $folder ) )->read( Folder::SORT_NAME, true );
//expected
$folderContent = (new Folder($folder))->read(Folder::SORT_NAME, true);
<rule ref="PSR2.Methods.FunctionCallSignature"/>
is broken for tab and unfortunately cannot be used.
This does not detect missing/too much indentation or space-usage instead of tabs.
Hi there :)
I have psr2r-sniffer installed (in a CakePHP 4 project), via composer, running PHP 7.3.29.
When I run the sniffer, I get the following error:
PHP Fatal error: Uncaught Error: Class 'PHP_CodeSniffer\Runner' not found in /home/sites/olive/htdocs/vendor/fig-r/psr2r-sniffer/PSR2R/Tools/Sniffer.php:73
Stack trace:
#0 /home/sites/olive/htdocs/vendor/fig-r/psr2r-sniffer/bin/sniff.php(23): PSR2R\Tools\Sniffer->sniff()
#1 {main}
thrown in /home/sites/olive/htdocs/vendor/fig-r/psr2r-sniffer/PSR2R/Tools/Sniffer.php on line 73
I have PHP_CodeSniffer installed through composer as well, and can run vendor/bin/phpcs path-to-file
without issues.
What could be wrong here? Thanks in advance!
As the title says what's the difference between them?
Hi,
I've got Code sniffer installed via composer (dev dependency) but I'm getting this issue. I'm running PHP 7.1
PHP Fatal error: Uncaught Error: Class 'PHP_CodeSniffer_CLI' not found in /vendor/fig-r/psr2r-sniffer/PSR2R/Tools/Sniffer.php:68
Stack trace:
#0 /vendor/fig-r/psr2r-sniffer/bin/sniff.php(22): PSR2R\Tools\Sniffer->sniff()
#1 {main}
thrown in /vendor/fig-r/psr2r-sniffer/PSR2R/Tools/Sniffer.php on line 68
[woohuiren@localhost atom-api]$ vendor/bin/sniff
PHP Fatal error: Uncaught Error: Class 'PHP_CodeSniffer_CLI' not found in /vendor/fig-r/psr2r-sniffer/PSR2R/Tools/Sniffer.php:68
Stack trace:
#0 /vendor/fig-r/psr2r-sniffer/bin/sniff.php(22): PSR2R\Tools\Sniffer->sniff()
#1 {main}
thrown in /vendor/fig-r/psr2r-sniffer/PSR2R/Tools/Sniffer.php on line 68\
I went to look at the file. Apparently its just use use PHP_CodeSniffer_CLI;
. Is there a better way to go about? I could possibly contribute something to allow the use of dev dependency version of PHP code sniffer.
Thanks again
EDIT: I took a look and apparenly #11 fixes this?
Weird behaviour occurs in sorting used classes in alphabetical order (see screenshot).
In my opinion a space comes before any letter...
To fix the notice it wants me to place Ad between AdAdvertiser and AdGroupLeadgeneration.
Suspect sniff: PSR2R\Sniffs\Namespaces\UseInAlphabeticalOrderSniff.php
PHP: 7.3.9
IDE: PhpStorm 2020.1.1
First
Then
See https://github.com/php-fig-rectified/psr2r-sniffer/wiki#php-version-map
Add: There must be no blank line before the namespace declaration.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.