-
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.
Initial work on a PSR-12 standard (ref #750)
- Loading branch information
Showing
11 changed files
with
602 additions
and
0 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/Keywords/ShortFormTypeKeywordsStandard.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="Short Form Type Keywords"> | ||
<standard> | ||
<![CDATA[ | ||
Short form of type keywords MUST be used i.e. bool instead of boolean, int instead of integer etc. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: Short form type used."> | ||
<![CDATA[ | ||
$foo = (bool) $isValid; | ||
]]> | ||
</code> | ||
<code title="Invalid: Long form type type used."> | ||
<![CDATA[ | ||
$foo = <em>(boolean)</em> $isValid; | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |
28 changes: 28 additions & 0 deletions
28
src/Standards/PSR12/Docs/Namespaces/CompoundNamespaceDepthStandard.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,28 @@ | ||
<documentation title="Compound Namespace Depth"> | ||
<standard> | ||
<![CDATA[ | ||
Compound namespaces with a depth of more than two MUST NOT be used. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: Max depth of 2."> | ||
<![CDATA[ | ||
use Vendor\Package\SomeNamespace\{ | ||
SubnamespaceOne\ClassA, | ||
SubnamespaceOne\ClassB, | ||
SubnamespaceTwo\ClassY, | ||
ClassZ, | ||
}; | ||
]]> | ||
</code> | ||
<code title="Invalid: Max depth of 3."> | ||
<![CDATA[ | ||
use Vendor\Package\SomeNamespace\{ | ||
<em>SubnamespaceOne\AnotherNamespace\ClassA,</em> | ||
SubnamespaceOne\ClassB, | ||
ClassZ, | ||
}; | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |
68 changes: 68 additions & 0 deletions
68
src/Standards/PSR12/Sniffs/Keywords/ShortFormTypeKeywordsSniff.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,68 @@ | ||
<?php | ||
/** | ||
* Verifies that the short form of type keywords is used (e.g., int, bool). | ||
* | ||
* @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\Keywords; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
class ShortFormTypeKeywordsSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_BOOL_CAST, | ||
T_INT_CAST, | ||
]; | ||
|
||
}//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(); | ||
|
||
if (($tokens[$stackPtr]['code'] === T_BOOL_CAST | ||
&& strtolower($tokens[$stackPtr]['content']) === '(bool)') | ||
|| ($tokens[$stackPtr]['code'] === T_INT_CAST | ||
&& strtolower($tokens[$stackPtr]['content']) === '(int)') | ||
) { | ||
return; | ||
} | ||
|
||
$error = 'Short form type keywords must be used'; | ||
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'LongFound'); | ||
if ($fix === true) { | ||
if ($tokens[$stackPtr]['code'] === T_BOOL_CAST) { | ||
$phpcsFile->fixer->replaceToken($stackPtr, '(bool)'); | ||
} else { | ||
$phpcsFile->fixer->replaceToken($stackPtr, '(int)'); | ||
} | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
80 changes: 80 additions & 0 deletions
80
src/Standards/PSR12/Sniffs/Namespaces/CompoundNamespaceDepthSniff.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,80 @@ | ||
<?php | ||
/** | ||
* Verifies that compound namespaces are not defined too deep. | ||
* | ||
* @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\Namespaces; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
class CompoundNamespaceDepthSniff implements Sniff | ||
{ | ||
|
||
/** | ||
* The max depth for compound namespaces. | ||
* | ||
* @var integer | ||
*/ | ||
public $maxDepth = 2; | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_OPEN_USE_GROUP]; | ||
|
||
}//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) | ||
{ | ||
$this->maxDepth = (int) $this->maxDepth; | ||
|
||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$end = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($stackPtr + 1)); | ||
if ($end === false) { | ||
return; | ||
} | ||
|
||
$depth = 1; | ||
for ($i = ($stackPtr + 1); $i <= $end; $i++) { | ||
if ($tokens[$i]['code'] === T_NS_SEPARATOR) { | ||
$depth++; | ||
continue; | ||
} | ||
|
||
if ($i === $end || $tokens[$i]['code'] === T_COMMA) { | ||
// End of a namespace. | ||
if ($depth > $this->maxDepth) { | ||
$error = 'Compound namespaces cannot have a depth more than %s'; | ||
$data = [$this->maxDepth]; | ||
$phpcsFile->addError($error, $i, 'TooDeep', $data); | ||
} | ||
|
||
$depth = 1; | ||
} | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
8 changes: 8 additions & 0 deletions
8
src/Standards/PSR12/Tests/Keywords/ShortFormTypeKeywordsUnitTest.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,8 @@ | ||
<?php | ||
$bar = (bool) $foo; | ||
$bar = (boolean) $foo; | ||
$bar = (Bool) $foo; | ||
$bar = (BOOLEAN) $foo; | ||
$bar = (int) $foo; | ||
$bar = (integer) $foo; | ||
$bar = (INT) $foo; |
8 changes: 8 additions & 0 deletions
8
src/Standards/PSR12/Tests/Keywords/ShortFormTypeKeywordsUnitTest.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,8 @@ | ||
<?php | ||
$bar = (bool) $foo; | ||
$bar = (bool) $foo; | ||
$bar = (Bool) $foo; | ||
$bar = (bool) $foo; | ||
$bar = (int) $foo; | ||
$bar = (int) $foo; | ||
$bar = (INT) $foo; |
52 changes: 52 additions & 0 deletions
52
src/Standards/PSR12/Tests/Keywords/ShortFormTypeKeywordsUnitTest.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,52 @@ | ||
<?php | ||
/** | ||
* Unit test class for the ShortFormTypeKeywords 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\Keywords; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class ShortFormTypeKeywordsUnitTest 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, | ||
5 => 1, | ||
7 => 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 |
31 changes: 31 additions & 0 deletions
31
src/Standards/PSR12/Tests/Namespaces/CompoundNamespaceDepthUnitTest.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,31 @@ | ||
<?php | ||
use Vendor\Package\SomeNamespace\{ | ||
SubnamespaceOne\ClassA, | ||
SubnamespaceOne\ClassB, | ||
SubnamespaceTwo\ClassY, | ||
ClassZ, | ||
}; | ||
|
||
use Vendor\Package\SomeNamespace\{ | ||
SubnamespaceOne\AnotherNamespace\ClassA, | ||
SubnamespaceOne\ClassB, | ||
ClassZ, | ||
}; | ||
|
||
use Vendor\Package\SomeNamespace\{ | ||
SubnamespaceOne /* comment */ | ||
\AnotherNamespace // comment | ||
\ClassA, | ||
SubnamespaceOne | ||
\AnotherNamespace | ||
\ClassB, | ||
ClassZ, | ||
}; | ||
|
||
// phpcs:set PSR12.Namespaces.CompoundNamespaceDepth maxDepth 3 | ||
|
||
use Vendor\Package\SomeNamespace\{ | ||
SubnamespaceOne\AnotherNamespace\ClassA, | ||
SubnamespaceOne\ClassB, | ||
ClassZ, | ||
}; |
52 changes: 52 additions & 0 deletions
52
src/Standards/PSR12/Tests/Namespaces/CompoundNamespaceDepthUnitTest.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,52 @@ | ||
<?php | ||
/** | ||
* Unit test class for the CompoundNamespaceDepth 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\Namespaces; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class CompoundNamespaceDepthUnitTest 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 [ | ||
10 => 1, | ||
18 => 1, | ||
21 => 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 |
Oops, something went wrong.