Skip to content

Commit

Permalink
Add doctrine/coding-standard
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jun 8, 2019
1 parent b43c73a commit aa2a35c
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/composer.lock
/phpunit.xml
/.phpunit.result.cache
/phpcs.xml
/.phpcs-cache
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ jobs:
- stage: Code Quality
php: 7.3
script: vendor/bin/phpstan analyse

- stage: Code Quality
php: 7.2
script: vendor/bin/phpcs
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"require-dev": {
"phpunit/phpunit": "^8.2",
"phpstan/phpstan": "^0.11.8"
"phpstan/phpstan": "^0.11.8",
"doctrine/coding-standard": "^6.0"
},
"autoload": {
"psr-4": { "Doctrine\\Common\\Lexer\\": "lib/Doctrine/Common/Lexer" }
Expand Down
105 changes: 48 additions & 57 deletions lib/Doctrine/Common/Lexer/AbstractLexer.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

declare(strict_types=1);

namespace Doctrine\Common\Lexer;

use ReflectionClass;
use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;
use const PREG_SPLIT_OFFSET_CAPTURE;
use function implode;
use function in_array;
use function preg_split;
use function sprintf;
use function substr;

/**
* Base class for writing simple lexers, i.e. for creating small DSLs.
*
* @since 2.0
* @author Guilherme Blanco <[email protected]>
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
*/
abstract class AbstractLexer
{
Expand All @@ -47,19 +37,19 @@ abstract class AbstractLexer
*
* @var array
*/
private $tokens = array();
private $tokens = [];

/**
* Current lexer position in input string.
*
* @var integer
* @var int
*/
private $position = 0;

/**
* Current peek of current lexer position.
*
* @var integer
* @var int
*/
private $peek = 0;

Expand Down Expand Up @@ -90,7 +80,7 @@ abstract class AbstractLexer
public function setInput($input)
{
$this->input = $input;
$this->tokens = array();
$this->tokens = [];

$this->reset();
$this->scan($input);
Expand All @@ -104,9 +94,9 @@ public function setInput($input)
public function reset()
{
$this->lookahead = null;
$this->token = null;
$this->peek = 0;
$this->position = 0;
$this->token = null;
$this->peek = 0;
$this->position = 0;
}

/**
Expand All @@ -122,7 +112,7 @@ public function resetPeek()
/**
* Resets the lexer position on the input to the given position.
*
* @param integer $position Position to place the lexical scanner.
* @param int $position Position to place the lexical scanner.
*
* @return void
*/
Expand All @@ -134,7 +124,7 @@ public function resetPosition($position = 0)
/**
* Retrieve the original lexer's input until a given position.
*
* @param integer $position
* @param int $position
*
* @return string
*/
Expand All @@ -146,37 +136,37 @@ public function getInputUntilPosition($position)
/**
* Checks whether a given token matches the current lookahead.
*
* @param integer|string $token
* @param int|string $token
*
* @return boolean
* @return bool
*/
public function isNextToken($token)
{
return null !== $this->lookahead && $this->lookahead['type'] === $token;
return $this->lookahead !== null && $this->lookahead['type'] === $token;
}

/**
* Checks whether any of the given tokens matches the current lookahead.
*
* @param array $tokens
*
* @return boolean
* @return bool
*/
public function isNextTokenAny(array $tokens)
{
return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true);
return $this->lookahead !== null && in_array($this->lookahead['type'], $tokens, true);
}

/**
* Moves to the next token in the input string.
*
* @return boolean
* @return bool
*/
public function moveNext()
{
$this->peek = 0;
$this->token = $this->lookahead;
$this->lookahead = (isset($this->tokens[$this->position]))
$this->peek = 0;
$this->token = $this->lookahead;
$this->lookahead = isset($this->tokens[$this->position])
? $this->tokens[$this->position++] : null;

return $this->lookahead !== null;
Expand All @@ -199,10 +189,10 @@ public function skipUntil($type)
/**
* Checks if given value is identical to the given token.
*
* @param mixed $value
* @param integer|string $token
* @param mixed $value
* @param int|string $token
*
* @return boolean
* @return bool
*/
public function isA($value, $token)
{
Expand All @@ -218,9 +208,9 @@ public function peek()
{
if (isset($this->tokens[$this->position + $this->peek])) {
return $this->tokens[$this->position + $this->peek++];
} else {
return null;
}

return null;
}

/**
Expand All @@ -230,8 +220,9 @@ public function peek()
*/
public function glimpse()
{
$peek = $this->peek();
$peek = $this->peek();
$this->peek = 0;

return $peek;
}

Expand All @@ -246,7 +237,7 @@ protected function scan($input)
{
static $regex;

if ( ! isset($regex)) {
if (! isset($regex)) {
$regex = sprintf(
'/(%s)|%s/%s',
implode(')|(', $this->getCatchablePatterns()),
Expand All @@ -255,37 +246,37 @@ protected function scan($input)
);
}

$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($regex, $input, -1, $flags);

if (false === $matches) {
if ($matches === false) {
// Work around https://bugs.php.net/78122
$matches = array(array($input, 0));
$matches = [[$input, 0]];
}

foreach ($matches as $match) {
// Must remain before 'value' assignment since it can change content
$type = $this->getType($match[0]);

$this->tokens[] = array(
$this->tokens[] = [
'value' => $match[0],
'type' => $type,
'position' => $match[1],
);
];
}
}

/**
* Gets the literal for a given token.
*
* @param integer|string $token
* @param int|string $token
*
* @return integer|string
* @return int|string
*/
public function getLiteral($token)
{
$className = get_class($this);
$reflClass = new \ReflectionClass($className);
$className = static::class;
$reflClass = new ReflectionClass($className);
$constants = $reflClass->getConstants();

foreach ($constants as $name => $value) {
Expand Down Expand Up @@ -326,7 +317,7 @@ abstract protected function getNonCatchablePatterns();
*
* @param string $value
*
* @return integer|string|null
* @return int|string|null
*/
abstract protected function getType(&$value);
}
28 changes: 28 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors" />

<!-- Ignore warnings and show progress of the run -->
<arg value="np"/>

<file>lib</file>
<file>tests</file>

<rule ref="Doctrine">
<!-- Traversable type hints often end up as mixed[], so we skip them for now -->
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification" />

<!-- Will cause BC breaks to method signatures - disabled for now -->
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint" />

<!-- Disabled to avoid class renaming - to be handled in a separate PR -->
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming" />
</rule>
</ruleset>
Loading

0 comments on commit aa2a35c

Please sign in to comment.