Skip to content

Commit

Permalink
Drop support for php < 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Dec 15, 2022
1 parent c391831 commit c4f371c
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
name: "PHPUnit"
uses: "doctrine/.github/.github/workflows/continuous-integration.yml@3.0.0"
with:
php-versions: '["7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]'
php-versions: '["8.1", "8.2"]'
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ awareness about deprecated code.
# Upgrade to 3.0.0

`Doctrine\Common\Lexer\Token` no longer implements `ArrayAccess`.
Parameter type declarations have been added to
`Doctrine\Common\Lexer\AbstractLexer` and `Doctrine\Common\Lexer\Token`.
You should add both parameter type declarations and return type declarations to
your lexers, based on the `@return` phpdoc.

# Upgrade to 2.0.0

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
],
"homepage": "https://www.doctrine-project.org/projects/lexer.html",
"require": {
"php": "^7.1 || ^8.0"
"php": "^8.1"
},
"require-dev": {
"doctrine/coding-standard": "^9 || ^10",
"phpstan/phpstan": "^1.3",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5",
"doctrine/coding-standard": "^10",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.18.3",
"vimeo/psalm": "^4.11 || ^5.0"
"vimeo/psalm": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 1 addition & 12 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors" />

<config name="php_version" value="70100"/>
<config name="php_version" value="80100"/>

<!-- Ignore warnings and show progress of the run -->
<arg value="np"/>
Expand All @@ -15,21 +15,10 @@
<file>tests</file>

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

<!-- Will cause BC breaks to method signatures - disabled for now -->
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint" />

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

<rule ref="SlevomatCodingStandard.Commenting.RequireOneLineDocComment.MultiLineDocComment">
<!-- Remove when dropping PHPUnit 7 -->
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
</MixedAssignment>
<MixedReturnStatement>
<errorLevel type="suppress">
<file name="src/AbstractLexer.php" />
<file name="src/Token.php" />
</errorLevel>
</MixedReturnStatement>
<ReferenceConstraintViolation>
<errorLevel type="suppress">
<!-- https://github.com/vimeo/psalm/issues/8891 -->
<file name="src/AbstractLexer.php" />
<file name="tests/EnumLexer.php" />
</errorLevel>
</ReferenceConstraintViolation>
<RedundantConditionGivenDocblockType>
Expand Down
54 changes: 19 additions & 35 deletions src/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ReflectionClass;
use UnitEnum;

use function get_class;
use function implode;
use function preg_split;
use function sprintf;
Expand All @@ -27,54 +26,46 @@ abstract class AbstractLexer
{
/**
* Lexer original input string.
*
* @var string
*/
private $input;
private string $input;

/**
* Array of scanned tokens.
*
* @var list<Token<T, V>>
*/
private $tokens = [];
private array $tokens = [];

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

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

/**
* The next token in the input.
*
* @var mixed[]|null
* @psalm-var Token<T, V>|null
*/
public $lookahead;
public Token|null $lookahead;

/**
* The last matched/seen token.
*
* @var mixed[]|null
* @psalm-var Token<T, V>|null
*/
public $token;
public Token|null $token;

/**
* Composed regex for input parsing.
*
* @var string|null
*/
private $regex;
private string|null $regex = null;

/**
* Sets the input data to be tokenized.
Expand All @@ -86,7 +77,7 @@ abstract class AbstractLexer
*
* @return void
*/
public function setInput($input)
public function setInput(string $input)
{
$this->input = $input;
$this->tokens = [];
Expand Down Expand Up @@ -125,19 +116,17 @@ public function resetPeek()
*
* @return void
*/
public function resetPosition($position = 0)
public function resetPosition(int $position = 0)
{
$this->position = $position;
}

/**
* Retrieve the original lexer's input until a given position.
*
* @param int $position
*
* @return string
*/
public function getInputUntilPosition($position)
public function getInputUntilPosition(int $position)
{
return substr($this->input, 0, $position);
}
Expand All @@ -151,7 +140,7 @@ public function getInputUntilPosition($position)
*
* @psalm-assert-if-true !=null $this->lookahead
*/
public function isNextToken($type)
public function isNextToken(int|string|UnitEnum $type)
{
return $this->lookahead !== null && $this->lookahead->isA($type);
}
Expand Down Expand Up @@ -194,7 +183,7 @@ public function moveNext()
*
* @return void
*/
public function skipUntil($type)
public function skipUntil(int|string|UnitEnum $type)
{
while ($this->lookahead !== null && ! $this->lookahead->isA($type)) {
$this->moveNext();
Expand All @@ -204,12 +193,9 @@ public function skipUntil($type)
/**
* Checks if given value is identical to the given token.
*
* @param string $value
* @param int|string $token
*
* @return bool
*/
public function isA($value, $token)
public function isA(string $value, int|string|UnitEnum $token)
{
return $this->getType($value) === $token;
}
Expand Down Expand Up @@ -250,14 +236,14 @@ public function glimpse()
*
* @return void
*/
protected function scan($input)
protected function scan(string $input)
{
if (! isset($this->regex)) {
$this->regex = sprintf(
'/(%s)|%s/%s',
implode(')|(', $this->getCatchablePatterns()),
implode('|', $this->getNonCatchablePatterns()),
$this->getModifiers()
$this->getModifiers(),
);
}

Expand All @@ -277,7 +263,7 @@ protected function scan($input)
$this->tokens[] = new Token(
$firstMatch,
$type,
$match[1]
$match[1],
);
}
}
Expand All @@ -289,10 +275,10 @@ protected function scan($input)
*
* @return int|string
*/
public function getLiteral($token)
public function getLiteral(int|string|UnitEnum $token)
{
if ($token instanceof UnitEnum) {
return get_class($token) . '::' . $token->name;
return $token::class . '::' . $token->name;
}

$className = static::class;
Expand Down Expand Up @@ -336,11 +322,9 @@ abstract protected function getNonCatchablePatterns();
/**
* Retrieve token type. Also processes the token value if necessary.
*
* @param string $value
*
* @return T|null
*
* @param-out V $value
*/
abstract protected function getType(&$value);
abstract protected function getType(string &$value);
}
7 changes: 3 additions & 4 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Token
* @readonly
* @var V
*/
public $value;
public string|int $value;

/**
* The type of the token (identifier, numeric, string, input parameter, none)
Expand All @@ -34,15 +34,14 @@ final class Token
* The position of the token in the input string
*
* @readonly
* @var int
*/
public $position;
public int $position;

/**
* @param V $value
* @param T|null $type
*/
public function __construct($value, $type, int $position)
public function __construct(string|int $value, $type, int $position)
{
$this->value = $value;
$this->type = $type;
Expand Down
26 changes: 8 additions & 18 deletions tests/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

class AbstractLexerTest extends TestCase
{
/** @var ConcreteLexer */
private $concreteLexer;
private ConcreteLexer $concreteLexer;

public function setUp(): void
{
Expand All @@ -29,9 +28,7 @@ public function tearDown(): void
setlocale(LC_ALL, null);
}

/**
* @psalm-return list<array{string, list<Token<string, string|int>>}>
*/
/** @psalm-return list<array{string, list<Token<string, string|int>>}> */
public function dataProvider(): array
{
return [
Expand Down Expand Up @@ -113,7 +110,7 @@ public function testSkipUntil(): void

$this->assertEquals(
new Token('=', 'operator', 5),
$this->concreteLexer->lookahead
$this->concreteLexer->lookahead,
);
}

Expand All @@ -125,7 +122,7 @@ public function testUtf8Mismatch(): void

$this->assertEquals(
new Token("\xE9=10", 'string', 0),
$this->concreteLexer->lookahead
$this->concreteLexer->lookahead,
);
}

Expand Down Expand Up @@ -172,23 +169,19 @@ public function testGlimpse(string $input, array $expectedTokens): void
$this->assertNull($this->concreteLexer->peek());
}

/**
* @psalm-return list<array{string, int, string}>
*/
/** @psalm-return list<array{string, int, string}> */
public function inputUntilPositionDataProvider(): array
{
return [
['price=10', 5, 'price'],
];
}

/**
* @dataProvider inputUntilPositionDataProvider
*/
/** @dataProvider inputUntilPositionDataProvider */
public function testGetInputUntilPosition(
string $input,
int $position,
string $expectedInput
string $expectedInput,
): void {
$this->concreteLexer->setInput($input);

Expand Down Expand Up @@ -242,15 +235,12 @@ public function testGetLiteral(): void
$this->assertSame('fake_token', $this->concreteLexer->getLiteral('fake_token'));
}

/**
* @requires PHP 8.1
*/
public function testGetLiteralWithEnumLexer(): void
{
$enumLexer = new EnumLexer();
$this->assertSame(
'Doctrine\Tests\Common\Lexer\TokenType::OPERATOR',
$enumLexer->getLiteral(TokenType::OPERATOR)
$enumLexer->getLiteral(TokenType::OPERATOR),
);
}

Expand Down
7 changes: 2 additions & 5 deletions tests/ConcreteLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/** @extends AbstractLexer<string, string|int> */
class ConcreteLexer extends AbstractLexer
{
public const INT = 'int';
final public const INT = 'int';

/**
* {@inheritDoc}
Expand All @@ -37,10 +37,7 @@ protected function getNonCatchablePatterns(): array
];
}

/**
* {@inheritDoc}
*/
protected function getType(&$value): string
protected function getType(string|int|float &$value): string
{
if (is_numeric($value)) {
$value = (int) $value;
Expand Down
Loading

0 comments on commit c4f371c

Please sign in to comment.