-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New sniff to check class instantiation for PSR12 + package.xml entrie…
…s for new sniffs (ref #750)
- Loading branch information
Showing
7 changed files
with
259 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Standards/PSR12/Docs/Classes/ClassInstantiationStandard.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<documentation title="Class Instantiation"> | ||
<standard> | ||
<![CDATA[ | ||
When instantiating a new class, parenthesis MUST always be present even when there are no arguments passed to the constructor. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: Parenthesis used."> | ||
<![CDATA[ | ||
new Foo(); | ||
]]> | ||
</code> | ||
<code title="Invalid: Parenthesis not used."> | ||
<![CDATA[ | ||
new Foo; | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |
82 changes: 82 additions & 0 deletions
82
src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Verifies that classes are instantiated with parenthesis. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
class ClassInstantiationSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_NEW]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in the | ||
* stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
// Find the class name. | ||
$allowed = [ | ||
T_STRING, | ||
T_NS_SEPARATOR, | ||
T_SELF, | ||
T_STATIC, | ||
T_VARIABLE, | ||
T_DOLLAR, | ||
]; | ||
|
||
$allowed += Tokens::$emptyTokens; | ||
|
||
$classNameEnd = $phpcsFile->findNext($allowed, ($stackPtr + 1), null, true); | ||
if ($classNameEnd === false) { | ||
return; | ||
} | ||
|
||
if ($tokens[$classNameEnd]['code'] === T_ANON_CLASS) { | ||
// Ignore anon classes. | ||
return; | ||
} | ||
|
||
if ($tokens[$classNameEnd]['code'] === T_OPEN_PARENTHESIS) { | ||
// Using parenthesis. | ||
return; | ||
} | ||
|
||
$error = 'Parenthesis must be used when instantiating a new class'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingParenthesis'); | ||
if ($fix === true) { | ||
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($classNameEnd - 1), null, true); | ||
$phpcsFile->fixer->addContent($prev, '()'); | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
25 changes: 25 additions & 0 deletions
25
src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
$foo = new Foo(); | ||
$foo = new Foo; | ||
$foo = new Foo\Bar; | ||
$foo = new Foo\Bar(); | ||
$foo = new Foo /* comment */ (); | ||
|
||
$foo = new $foo(); | ||
$foo = new $foo; | ||
$foo = new $$foo(); | ||
$foo = new $$foo; | ||
|
||
$foo = new self(); | ||
$foo = new self; | ||
$foo = new static(); | ||
$foo = new static; | ||
|
||
foo(new class {}); | ||
echo (new Foo())->bar(); | ||
echo (new Foo)->bar(); | ||
echo (new Foo((new Bar)->getBaz()))->bar(); | ||
$foo = (new Foo)::$bar; | ||
|
||
echo (new Foo((new Bar//comment | ||
)->getBaz(new Baz /* comment */)))->bar(); |
25 changes: 25 additions & 0 deletions
25
src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
$foo = new Foo(); | ||
$foo = new Foo(); | ||
$foo = new Foo\Bar(); | ||
$foo = new Foo\Bar(); | ||
$foo = new Foo /* comment */ (); | ||
|
||
$foo = new $foo(); | ||
$foo = new $foo(); | ||
$foo = new $$foo(); | ||
$foo = new $$foo(); | ||
|
||
$foo = new self(); | ||
$foo = new self(); | ||
$foo = new static(); | ||
$foo = new static(); | ||
|
||
foo(new class {}); | ||
echo (new Foo())->bar(); | ||
echo (new Foo())->bar(); | ||
echo (new Foo((new Bar())->getBaz()))->bar(); | ||
$foo = (new Foo())::$bar; | ||
|
||
echo (new Foo((new Bar()//comment | ||
)->getBaz(new Baz() /* comment */)))->bar(); |
60 changes: 60 additions & 0 deletions
60
src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Unit test class for the ClassInstantiation sniff. | ||
* | ||
* @author Greg Sherwood <gsherwood@squiz.net> | ||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\PSR12\Tests\Classes; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class ClassInstantiationUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return [ | ||
3 => 1, | ||
4 => 1, | ||
9 => 1, | ||
11 => 1, | ||
14 => 1, | ||
16 => 1, | ||
20 => 1, | ||
21 => 1, | ||
22 => 1, | ||
24 => 1, | ||
25 => 1, | ||
]; | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters