diff --git a/.gitignore b/.gitignore index 57872d0..e1100b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/vendor/ +vendor/ +cache/* +composer.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ddde192 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +language: php + +dist: trusty + +cache: + directories: + - $HOME/.composer/cache/files + +php: + - 5.5 + - 5.6 + - 7 + - 7.1 + - hhvm + - nightly + +matrix: + fast_finish: true + +before_install: + - sudo apt-get update + - sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce + +before_script: + - travis_retry composer update --no-interaction + - mkdir -p cache + +script: + - vendor/bin/phpcs -p --warning-severity=0 --cache=cache/phpcs --parallel=10 PHP/ examples/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0c3213e..6bfd0ff 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,6 +3,6 @@ This is the process for submitting a proposed change to any standards or examples in this repository: 1. Create a branch and make the desired changes. -2. Open a Pull Request from your branch into the master branch. -3. The rest of the team will then debate and amend the change and ultimately decide if it should be included. -4. It will be merged if the majority of the team agrees with the change. +1. Open a Pull Request from your branch into the master branch. +1. The rest of the team will then debate and amend the change and ultimately decide if it should be included. +1. It will be merged if the majority of the team agrees with the change. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2377269 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +build: + @docker run --rm -it \ + -v $$(pwd):/usr/src/app \ + -v ~/.composer:/home/composer/.composer \ + -v ~/.ssh/id_rsa:/home/composer/.ssh/id_rsa:ro \ + graze/composer install + +lint: + @mkdir -p cache + @docker run --rm -it -v $$(pwd):/srv:cached graze/php-alpine:test vendor/bin/phpcs \ + -p --warning-severity=0 --cache=cache/phpcs --parallel=10 \ + PHP/ examples/ diff --git a/PHP/CodeSniffer/Graze/Sniffs/Commenting/InlineVariableCommentSniff.php b/PHP/CodeSniffer/Graze/Sniffs/Commenting/InlineVariableCommentSniff.php index 90fd294..ea24530 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/Commenting/InlineVariableCommentSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/Commenting/InlineVariableCommentSniff.php @@ -1,44 +1,50 @@ getTokens(); - $commentToken = array( + $commentToken = [ T_COMMENT, T_DOC_COMMENT_CLOSE_TAG, - ); + ]; $commentEnd = $phpcsFile->findPrevious($commentToken, $stackPtr); $commentStart = $tokens[$commentEnd]['comment_opener']; if ($tokens[$commentEnd]['line'] === $tokens[$commentStart]['line']) { - $phpcsFile->addError('Member variable comment should not be inline', $stackPtr); + $phpcsFile->addError('Member variable comment should not be inline', $stackPtr, static::ERROR_CODE); } } /** * Called to process normal member vars. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this + * @param File $phpcsFile The PHP_CodeSniffer file where this * token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processVariable( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { } @@ -48,7 +54,7 @@ protected function processVariable( * Note that there may be more than one variable in the string, which will * result only in one call for the string or one call per line for heredocs. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this + * @param File $phpcsFile The PHP_CodeSniffer file where this * token was found. * @param int $stackPtr The position where the double quoted * string was found. @@ -56,7 +62,7 @@ protected function processVariable( * @return void */ protected function processVariableInString( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { } diff --git a/PHP/CodeSniffer/Graze/Sniffs/Commenting/InvalidTypeSniff.php b/PHP/CodeSniffer/Graze/Sniffs/Commenting/InvalidTypeSniff.php index d09832a..b7d9f4f 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/Commenting/InvalidTypeSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/Commenting/InvalidTypeSniff.php @@ -1,7 +1,13 @@ getTokens(); $commentToken = [ @@ -61,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $type = array_shift($typeBits); if (array_key_exists($type, static::$types)) { - $fix = $phpcsFile->addFixableError('Type should be \'' . static::$types[$type] . '\' not \'' . $type . '\'', $tag); + $fix = $phpcsFile->addFixableError('Type should be \'' . static::$types[$type] . '\' not \'' . $type . '\'', $tag, static::ERROR_CODE); if ($fix) { $content = trim(static::$types[$type] . ' ' . implode(' ', $typeBits)); $phpcsFile->fixer->replaceToken(($tag + 2), $content); diff --git a/PHP/CodeSniffer/Graze/Sniffs/Commenting/MissingFunctionCommentSniff.php b/PHP/CodeSniffer/Graze/Sniffs/Commenting/MissingFunctionCommentSniff.php index ce1b505..b25aabb 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/Commenting/MissingFunctionCommentSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/Commenting/MissingFunctionCommentSniff.php @@ -1,7 +1,16 @@ + * * @return int[] * @see Tokens.php */ public function register() { return [ - T_FUNCTION + T_FUNCTION, ]; } /** - * @param \PHP_CodeSniffer_File $phpcsFile - * @param int $stackPtr + * @param File $phpcsFile + * @param int $stackPtr + * + * @return int|void */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $find = PHP_CodeSniffer_Tokens::$methodPrefixes; + $find = Tokens::$methodPrefixes; $find[] = T_WHITESPACE; $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); @@ -49,24 +61,25 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT ) { - if (array_key_exists('scope_opener', $tokens[$stackPtr]) && $this->functionHasReturn($phpcsFile, $stackPtr)) { - $phpcsFile->addError('Function has return keyword but no doc block', $stackPtr); + if (array_key_exists('scope_opener', $tokens[$stackPtr]) + && $this->functionHasReturn($phpcsFile, $stackPtr)) { + $phpcsFile->addError('Function has return keyword but no doc block', $stackPtr, static::ERROR_CODE_RETURN); return; } if ($this->functionHasParams($phpcsFile, $stackPtr)) { - $phpcsFile->addError('Function has parameters but no doc block', $stackPtr); + $phpcsFile->addError('Function has parameters but no doc block', $stackPtr, static::ERROR_CODE_PARAMETER); } } } /** - * @param \PHP_CodeSniffer_File $phpcsFile - * @param int $stackPtr + * @param File $phpcsFile + * @param int $stackPtr * * @return bool */ - private function functionHasReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + private function functionHasReturn(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $start = $tokens[$stackPtr]['scope_opener']; @@ -79,7 +92,7 @@ private function functionHasReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } // if we hit a return keyword that isn't immediately followed by a semicolon, we need a doc block - if ($tokens[$i]['code'] === T_RETURN && $tokens[$i+1]['code'] !== T_SEMICOLON) { + if ($tokens[$i]['code'] === T_RETURN && $tokens[$i + 1]['code'] !== T_SEMICOLON) { return true; } } @@ -88,13 +101,14 @@ private function functionHasReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } /** - * @param PHP_CodeSniffer_File $phpcsFile - * @param $stackPtr + * @param File $phpcsFile + * @param int $stackPtr * * @return bool + * @throws \PHP_CodeSniffer\Exceptions\TokenizerException */ - private function functionHasParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + private function functionHasParams(File $phpcsFile, $stackPtr) { - return (bool) $phpcsFile->getMethodParameters($stackPtr); + return (bool)$phpcsFile->getMethodParameters($stackPtr); } } diff --git a/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/IfVariableAssignmentSniff.php b/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/IfVariableAssignmentSniff.php index b4325ec..7f3b996 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/IfVariableAssignmentSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/IfVariableAssignmentSniff.php @@ -1,7 +1,13 @@ addError('Encountered an error', $stackPtr); * * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * @param File $phpcsFile The PHP_CodeSniffer file where the * token was found. * @param int $stackPtr The position in the PHP_CodeSniffer * file's token stack where the token @@ -60,7 +66,7 @@ public function register() * pointer is reached. Return (count($tokens) + 1) to skip * the rest of the file. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); @@ -69,7 +75,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) for ($i = $start; $i <= $end; $i++) { if ($tokens[$i]['code'] === T_EQUAL) { - $phpcsFile->addError('Variable assigment in if statement', $stackPtr); + $phpcsFile->addError('Variable assigment in if statement', $stackPtr, static::ERROR_CODE); } } } diff --git a/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/NestedTernarySniff.php b/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/NestedTernarySniff.php index 81a81a4..22fadcd 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/NestedTernarySniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/ControlStructures/NestedTernarySniff.php @@ -1,7 +1,13 @@ addError('Encountered an error', $stackPtr); * * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * @param File $phpcsFile The PHP_CodeSniffer file where the * token was found. * @param int $stackPtr The position in the PHP_CodeSniffer * file's token stack where the token @@ -61,7 +67,7 @@ public function register() * pointer is reached. Return (count($tokens) + 1) to skip * the rest of the file. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); @@ -70,7 +76,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) for ($i = $start; $i <= $end; $i++) { if ($tokens[$i]['code'] === T_INLINE_THEN) { - $phpcsFile->addError('Nested ternary operator is not allowed', $stackPtr); + $phpcsFile->addError('Nested ternary operator is not allowed', $stackPtr, static::ERROR_CODE); } } } diff --git a/PHP/CodeSniffer/Graze/Sniffs/Files/DoubleBlankLineSniff.php b/PHP/CodeSniffer/Graze/Sniffs/Files/DoubleBlankLineSniff.php index 565f4af..12efd56 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/Files/DoubleBlankLineSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/Files/DoubleBlankLineSniff.php @@ -1,7 +1,14 @@ addError('Encountered an error', $stackPtr); * * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * @param File $phpcsFile The PHP_CodeSniffer file where the * token was found. * @param int $stackPtr The position in the PHP_CodeSniffer * file's token stack where the token @@ -60,7 +67,7 @@ public function register() * pointer is reached. Return (count($tokens) + 1) to skip * the rest of the file. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); @@ -71,7 +78,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $nextWhitespace = $tokens[$nextWhitespace]; if ($nextWhitespace['column'] === 1 && $nextWhitespace['length'] === 0 && $nextWhitespace['line'] - $currentLineNumber === 1) { // double blank line - $phpcsFile->addError('Multiple blank lines detected', $stackPtr); + $phpcsFile->addError('Multiple blank lines detected', $stackPtr, static::ERROR_CODE); } } } diff --git a/PHP/CodeSniffer/Graze/Sniffs/Naming/AbstractClassNamingSniff.php b/PHP/CodeSniffer/Graze/Sniffs/Naming/AbstractClassNamingSniff.php index 24c7b14..88d53da 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/Naming/AbstractClassNamingSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/Naming/AbstractClassNamingSniff.php @@ -1,7 +1,14 @@ addError('Encountered an error', $stackPtr); * * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * @param File $phpcsFile The PHP_CodeSniffer file where the * token was found. * @param int $stackPtr The position in the PHP_CodeSniffer * file's token stack where the token @@ -60,7 +67,7 @@ public function register() * pointer is reached. Return (count($tokens) + 1) to skip * the rest of the file. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); @@ -68,7 +75,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $className = $phpcsFile->getDeclarationName($stackPtr); if (substr($className, 0, 8) !== 'Abstract') { - $phpcsFile->addError('Abstract class name "%s" must be prefixed with "Abstract"', $stackPtr, '', [$className]); + $phpcsFile->addError('Abstract class name "%s" must be prefixed with "Abstract"', $stackPtr, static::ERROR_CODE, '', [$className]); } } } diff --git a/PHP/CodeSniffer/Graze/Sniffs/Naming/InterfaceNamingSniff.php b/PHP/CodeSniffer/Graze/Sniffs/Naming/InterfaceNamingSniff.php index 1093b37..d41be41 100644 --- a/PHP/CodeSniffer/Graze/Sniffs/Naming/InterfaceNamingSniff.php +++ b/PHP/CodeSniffer/Graze/Sniffs/Naming/InterfaceNamingSniff.php @@ -1,7 +1,13 @@ addError('Encountered an error', $stackPtr); * * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * @param File $phpcsFile The PHP_CodeSniffer file where the * token was found. * @param int $stackPtr The position in the PHP_CodeSniffer * file's token stack where the token @@ -61,12 +67,12 @@ public function register() * pointer is reached. Return (count($tokens) + 1) to skip * the rest of the file. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { $className = $phpcsFile->getDeclarationName($stackPtr); if (substr($className, -9) !== 'Interface') { - $phpcsFile->addError('Interface name "%s" must be suffixed with "Interface"', $stackPtr, '', [$className]); + $phpcsFile->addError('Interface name "%s" must be suffixed with "Interface"', $stackPtr, static::ERROR_CODE, '', [$className]); } } } diff --git a/composer.json b/composer.json index e21d478..9c97265 100644 --- a/composer.json +++ b/composer.json @@ -10,9 +10,12 @@ "license": "MIT", "authors": [ { - "name": "Will Pillar", - "email": "will.pillar@graze.com" + "name": "Graze Developers", + "email": "developers@graze.com" } ], - "require": {} + "require": {}, + "require-dev": { + "squizlabs/php_codesniffer": "^3.0" + } } diff --git a/examples/PHP/class.php b/examples/PHP/class.php index a177415..6980158 100644 --- a/examples/PHP/class.php +++ b/examples/PHP/class.php @@ -2,7 +2,8 @@ namespace Graze\Example; // namespace declaration with blank line above -use Graze\Lib\Foo; // use statements with blank line above +// use statements with blank line above +use Foo; class Example { @@ -28,10 +29,15 @@ public function getFoo() } /** - * @param Foo $foo - * @param string $bar - * @param string $baz - * @param string $bat + * @param Foo $foo + * @param string $bar + * @param string $baz + * @param string $bat + * @param string|null $bak + * @param string|null $bas + * @param string|null $bab + * @param string|null $bap + * @param string|null $bad */ public function methodWithManyArguments( Foo $foo, diff --git a/examples/PHP/controlStatements.php b/examples/PHP/controlStatements.php index 4245a4f..29875b7 100644 --- a/examples/PHP/controlStatements.php +++ b/examples/PHP/controlStatements.php @@ -2,31 +2,32 @@ // basic if - else if ($expr1) { - // if body + echo 'if body'; } elseif ($expr2) { - // elseif body + echo 'elseif body'; } else { - // else body; + echo 'else body'; } // if with a negation if (!$expr1) { // if body + echo 'negative if body'; } // if with an operand if ($expr1 && $expr2) { - // if body + echo 'if body'; } else { - // else body; + echo 'else body'; } // if with long expression -if ($expr1 && - $expr2 && - $expr3 && - $expr4 &&) { - // if body +if ($expr1 + && $expr2 + && $expr3 + && $expr4) { + echo 'if body'; } // switch statement @@ -49,19 +50,22 @@ // for loop for ($i = 0; $i < 10; $i++) { - // for body + echo 'for body ' . $i; } // foreach loop foreach ($iterable as $key => $value) { - // foreach body + echo 'foreach body'; } // try - catch try { - // try body + echo 'try body'; } catch (FirstExceptionType $e) { - // catch body + echo 'catch body'; } catch (OtherExceptionType $e) { - // catch body + echo 'other catch body'; } + +// casting +$int = (int) $string; diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..8b32171 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,9 @@ + + + The graze PHP coding standard as defined in graze/standards. + + + + + +