Skip to content

Commit

Permalink
Initial work on a PSR-12 standard (ref #750)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Apr 19, 2018
1 parent d0ee54f commit c61bba3
Show file tree
Hide file tree
Showing 11 changed files with 602 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
--- If the return type contains namespace information, it will be cleaned of whitespace and comments
---- To access the original return value string, use the main tokens array

- This release contains an incomplete version of the PSR-12 coding standard
-- Errors found using this standard should be valid, but it will miss a lot of violations until it is complete
-- If you'd like to test and help, you can use the standard by running PHPCS with --standard=PSR12

- Config values set using --runtime-set now override any config values set in rulesets or the CodeSniffer.conf file
- You can now apply include-pattern rules to individual message codes in a ruleset like you can with exclude-pattern rules
-- Previously, include-pattern rules only applied to entire sniffs
Expand Down Expand Up @@ -107,6 +111,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Allows the required spacing to be set using the spacing sniff property (default is 0)
-- Allows newlines to be used by setting the ignoreNewlines sniff property (default is false)
-- Thanks to Juliette Reinders Folmer for the contribution
- Added New PSR12.Keywords.ShortFormTypeKeywords sniff
-- Ensures the short form of PHP types is used when type casting
- Added New PSR12.Namespaces.CompundNamespaceDepth sniff
-- Ensures compund namespace use statements have a max depth of 2 levels
-- The max depth can be changed by setting the 'maxDepth' sniff property in a ruleset.xml file
- Improved core support for grouped property declarations
-- Also improves support in Squiz.WhiteSpace.ScopeKeywordSpacing and Squiz.WhiteSpace.MemberVarSpacing
-- Thanks to Juliette Reinders Folmer for the patch
Expand Down
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>
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 src/Standards/PSR12/Sniffs/Keywords/ShortFormTypeKeywordsSniff.php
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
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
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;
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;
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
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,
};
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
Loading

0 comments on commit c61bba3

Please sign in to comment.