Skip to content

Commit

Permalink
update phpcs to 3.0 (#29)
Browse files Browse the repository at this point in the history
* update phpcs to 3.0

* use official phpcs v3 version

* little changes

* add travis build info

* remove composer.lock

* remove un-needed assignments

* ensure cache directory exists

* ensure cache directory exists in travis
  • Loading branch information
Harry Bragg authored Aug 23, 2017
1 parent 1dd0e6b commit 4afc1e2
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 78 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
vendor/
cache/*
composer.lock
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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/
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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/
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
<?php

class Graze_Sniffs_Commenting_InlineVariableCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
namespace Graze\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;

class InlineVariableCommentSniff extends AbstractVariableSniff
{
const ERROR_CODE = 'graze.commenting.inlineVariableComment';
/**
* Called to process class 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 processMemberVar(
PHP_CodeSniffer_File $phpcsFile,
File $phpcsFile,
$stackPtr
) {
$tokens = $phpcsFile->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
) {
}
Expand All @@ -48,15 +54,15 @@ 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.
*
* @return void
*/
protected function processVariableInString(
PHP_CodeSniffer_File $phpcsFile,
File $phpcsFile,
$stackPtr
) {
}
Expand Down
14 changes: 10 additions & 4 deletions PHP/CodeSniffer/Graze/Sniffs/Commenting/InvalidTypeSniff.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

class Graze_Sniffs_Commenting_InvalidTypeSniff implements PHP_CodeSniffer_Sniff
namespace Graze\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class InvalidTypeSniff implements Sniff
{
const ERROR_CODE = 'graze.commenting.invalidType';
/**
* @var array
*/
Expand Down Expand Up @@ -31,10 +37,10 @@ public function register()
}

/**
* @param \PHP_CodeSniffer_File $phpcsFile
* @param File $phpcsFile
* @param int $stackPtr
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$commentToken = [
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php

class Graze_Sniffs_Commenting_MissingFunctionCommentSniff implements PHP_CodeSniffer_Sniff
namespace Graze\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class MissingFunctionCommentSniff implements Sniff
{
const ERROR_CODE_RETURN = 'graze.commenting.missingFunctionComment.returnNoDoc';
const ERROR_CODE_PARAMETER = 'graze.commenting.missingFunctionComment.parameterNoDoc';

/**
* Registers the tokens that this sniff wants to listen for.
* An example return value for a sniff that wants to listen for whitespace
Expand All @@ -13,25 +22,28 @@ class Graze_Sniffs_Commenting_MissingFunctionCommentSniff implements PHP_CodeSni
* T_COMMENT,
* );
* </code>
*
* @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);
Expand All @@ -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'];
Expand All @@ -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;
}
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

class Graze_Sniffs_ControlStructures_IfVariableAssignmentSniff implements PHP_CodeSniffer_Sniff
namespace Graze\Sniffs\ControlStructures;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class IfVariableAssignmentSniff implements Sniff
{
const ERROR_CODE = 'graze.controlStructures.ifVariableAssignment';
/**
* Registers the tokens that this sniff wants to listen for.
*
Expand Down Expand Up @@ -49,7 +55,7 @@ public function register()
* $phpcsFile->addError('Encountered an error', $stackPtr);
* </code>
*
* @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
Expand All @@ -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();

Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

class Graze_Sniffs_ControlStructures_NestedTernarySniff implements PHP_CodeSniffer_Sniff
namespace Graze\Sniffs\ControlStructures;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class NestedTernarySniff implements Sniff
{
const ERROR_CODE = 'graze.controlStructures.nestedTernary';

/**
* Registers the tokens that this sniff wants to listen for.
Expand Down Expand Up @@ -50,7 +56,7 @@ public function register()
* $phpcsFile->addError('Encountered an error', $stackPtr);
* </code>
*
* @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
Expand All @@ -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();

Expand All @@ -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);
}
}
}
Expand Down
Loading

0 comments on commit 4afc1e2

Please sign in to comment.