composer update

This commit is contained in:
2019-12-01 06:37:45 +00:00
parent fa199eef05
commit 3115ab75a5
3650 changed files with 72361 additions and 147137 deletions

View File

@@ -0,0 +1,125 @@
Introduction
============
The Doctrine Inflector has static methods for inflecting text.
The features include pluralization, singularization,
converting between camelCase and under_score and capitalizing
words.
All you need to use the Inflector is the ``Doctrine\Common\Inflector\Inflector``
class.
Installation
============
You can install the Inflector with composer:
.. code-block::
$ composer require doctrine/inflector
Here are the available methods that you can use:
Tableize
========
Converts ``ModelName`` to ``model_name``:
.. code-block:: php
echo Inflector::tableize('ModelName'); // model_name
Classify
========
Converts ``model_name`` to ``ModelName``:
.. code-block:: php
echo Inflector::classify('model_name'); // ModelName
Camelize
========
This method uses `Classify`_ and then converts the first character to lowercase:
.. code-block:: php
echo Inflector::camelize('model_name'); // modelName
ucwords
=======
Takes a string and capitalizes all of the words, like PHP's built-in
ucwords function. This extends that behavior, however, by allowing the
word delimiters to be configured, rather than only separating on
whitespace.
Here is an example:
.. code-block:: php
$string = 'top-o-the-morning to all_of_you!';
echo Inflector::ucwords($string); // Top-O-The-Morning To All_of_you!
echo Inflector::ucwords($string, '-_ '); // Top-O-The-Morning To All_Of_You!
Pluralize
=========
Returns a word in plural form.
.. code-block:: php
echo Inflector::pluralize('browser'); // browsers
Singularize
===========
.. code-block:: php
echo Inflector::singularize('browsers'); // browser
Rules
=====
Customize the rules for pluralization and singularization:
.. code-block:: php
Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
Inflector::rules('plural', [
'rules' => ['/^(inflect)ors$/i' => '\1ables'],
'uninflected' => ['dontinflectme'],
'irregular' => ['red' => 'redlings']
]);
The arguments for the ``rules`` method are:
- ``$type`` - The type of inflection, either ``plural`` or ``singular``
- ``$rules`` - An array of rules to be added.
- ``$reset`` - If true, will unset default inflections for all new rules that are being defined in $rules.
Reset
=====
Clears Inflectors inflected value caches, and resets the inflection
rules to the initial values.
.. code-block:: php
Inflector::reset();
Slugify
=======
You can easily use the Inflector to create a slug from a string of text
by using the `tableize`_ method and replacing underscores with hyphens:
.. code-block:: php
public static function slugify(string $text) : string
{
return str_replace('_', '-', Inflector::tableize($text));
}

View File

@@ -85,6 +85,7 @@ class Inflector
'beef' => 'beefs',
'brother' => 'brothers',
'cafe' => 'cafes',
'canvas' => 'canvases',
'chateau' => 'chateaux',
'niveau' => 'niveaux',
'child' => 'children',
@@ -99,6 +100,7 @@ class Inflector
'foot' => 'feet',
'fungus' => 'fungi',
'ganglion' => 'ganglions',
'gas' => 'gases',
'genie' => 'genies',
'genus' => 'genera',
'goose' => 'geese',

View File

@@ -1,3 +1,3 @@
patreon: phpdoctrine
tidelift: packagist/doctrine%2Flexer
tidelift: packagist/doctrine%2Finstantiator
custom: https://www.doctrine-project.org/sponsorship.html

View File

@@ -2,12 +2,15 @@
namespace Doctrine\Instantiator;
use ArrayIterator;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
use Doctrine\Instantiator\Exception\UnexpectedValueException;
use Exception;
use ReflectionClass;
use ReflectionException;
use Serializable;
use function class_exists;
use function is_subclass_of;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
@@ -94,7 +97,7 @@ final class Instantiator implements InstantiatorInterface
$serializedString = sprintf(
'%s:%d:"%s":0:{}',
self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER,
is_subclass_of($className, Serializable::class) ? self::SERIALIZATION_FORMAT_USE_UNSERIALIZER : self::SERIALIZATION_FORMAT_AVOID_UNSERIALIZER,
strlen($className),
$className
);
@@ -107,12 +110,10 @@ final class Instantiator implements InstantiatorInterface
}
/**
* @param string $className
*
* @throws InvalidArgumentException
* @throws ReflectionException
*/
private function getReflectionClass($className) : ReflectionClass
private function getReflectionClass(string $className) : ReflectionClass
{
if (! class_exists($className)) {
throw InvalidArgumentException::fromNonExistingClass($className);
@@ -132,7 +133,7 @@ final class Instantiator implements InstantiatorInterface
*/
private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, string $serializedString) : void
{
set_error_handler(static function ($code, $message, $file, $line) use ($reflectionClass, & $error) : void {
set_error_handler(static function (int $code, string $message, string $file, int $line) use ($reflectionClass, &$error) : bool {
$error = UnexpectedValueException::fromUncleanUnSerialization(
$reflectionClass,
$message,
@@ -140,6 +141,8 @@ final class Instantiator implements InstantiatorInterface
$file,
$line
);
return true;
});
try {
@@ -193,6 +196,8 @@ final class Instantiator implements InstantiatorInterface
*/
private function isSafeToClone(ReflectionClass $reflection) : bool
{
return $reflection->isCloneable() && ! $reflection->hasMethod('__clone');
return $reflection->isCloneable()
&& ! $reflection->hasMethod('__clone')
&& ! $reflection->isSubclassOf(ArrayIterator::class);
}
}

View File

@@ -1,17 +0,0 @@
{
"active": true,
"name": "Lexer",
"slug": "lexer",
"docsSlug": "doctrine-lexer",
"versions": [
{
"name": "master",
"branchName": "master",
"slug": "latest",
"aliases": [
"current",
"stable"
]
}
]
}

View File

@@ -1,2 +0,0 @@
vendor
composer.lock

View File

@@ -1,5 +1,7 @@
# Doctrine Lexer
Build Status: [![Build Status](https://travis-ci.org/doctrine/lexer.svg?branch=master)](https://travis-ci.org/doctrine/lexer)
Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
This lexer is used in Doctrine Annotations and in Doctrine ORM (DQL).

View File

@@ -17,10 +17,12 @@
{"name": "Johannes Schmitt", "email": "schmittjoh@gmail.com"}
],
"require": {
"php": ">=5.3.2"
"php": "^7.2"
},
"require-dev": {
"phpunit/phpunit": "^4.5"
"doctrine/coding-standard": "^6.0",
"phpstan/phpstan": "^0.11.8",
"phpunit/phpunit": "^8.2"
},
"autoload": {
"psr-4": { "Doctrine\\Common\\Lexer\\": "lib/Doctrine/Common/Lexer" }
@@ -30,7 +32,10 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.2.x-dev"
}
},
"config": {
"sort-packages": true
}
}

View File

@@ -1,294 +0,0 @@
DQL Lexer
=========
Here is a more complicated example from the Doctrine ORM project.
The ``Doctrine\ORM\Query\Lexer`` implementation for DQL looks something
like the following:
.. code-block:: php
use Doctrine\Common\Lexer\AbstractLexer;
class Lexer extends AbstractLexer
{
// All tokens that are not valid identifiers must be < 100
public const T_NONE = 1;
public const T_INTEGER = 2;
public const T_STRING = 3;
public const T_INPUT_PARAMETER = 4;
public const T_FLOAT = 5;
public const T_CLOSE_PARENTHESIS = 6;
public const T_OPEN_PARENTHESIS = 7;
public const T_COMMA = 8;
public const T_DIVIDE = 9;
public const T_DOT = 10;
public const T_EQUALS = 11;
public const T_GREATER_THAN = 12;
public const T_LOWER_THAN = 13;
public const T_MINUS = 14;
public const T_MULTIPLY = 15;
public const T_NEGATE = 16;
public const T_PLUS = 17;
public const T_OPEN_CURLY_BRACE = 18;
public const T_CLOSE_CURLY_BRACE = 19;
// All tokens that are identifiers or keywords that could be considered as identifiers should be >= 100
public const T_ALIASED_NAME = 100;
public const T_FULLY_QUALIFIED_NAME = 101;
public const T_IDENTIFIER = 102;
// All keyword tokens should be >= 200
public const T_ALL = 200;
public const T_AND = 201;
public const T_ANY = 202;
public const T_AS = 203;
public const T_ASC = 204;
public const T_AVG = 205;
public const T_BETWEEN = 206;
public const T_BOTH = 207;
public const T_BY = 208;
public const T_CASE = 209;
public const T_COALESCE = 210;
public const T_COUNT = 211;
public const T_DELETE = 212;
public const T_DESC = 213;
public const T_DISTINCT = 214;
public const T_ELSE = 215;
public const T_EMPTY = 216;
public const T_END = 217;
public const T_ESCAPE = 218;
public const T_EXISTS = 219;
public const T_FALSE = 220;
public const T_FROM = 221;
public const T_GROUP = 222;
public const T_HAVING = 223;
public const T_HIDDEN = 224;
public const T_IN = 225;
public const T_INDEX = 226;
public const T_INNER = 227;
public const T_INSTANCE = 228;
public const T_IS = 229;
public const T_JOIN = 230;
public const T_LEADING = 231;
public const T_LEFT = 232;
public const T_LIKE = 233;
public const T_MAX = 234;
public const T_MEMBER = 235;
public const T_MIN = 236;
public const T_NEW = 237;
public const T_NOT = 238;
public const T_NULL = 239;
public const T_NULLIF = 240;
public const T_OF = 241;
public const T_OR = 242;
public const T_ORDER = 243;
public const T_OUTER = 244;
public const T_PARTIAL = 245;
public const T_SELECT = 246;
public const T_SET = 247;
public const T_SOME = 248;
public const T_SUM = 249;
public const T_THEN = 250;
public const T_TRAILING = 251;
public const T_TRUE = 252;
public const T_UPDATE = 253;
public const T_WHEN = 254;
public const T_WHERE = 255;
public const T_WITH = 256;
/**
* Creates a new query scanner object.
*
* @param string $input A query string.
*/
public function __construct($input)
{
$this->setInput($input);
}
/**
* {@inheritdoc}
*/
protected function getCatchablePatterns()
{
return [
'[a-z_][a-z0-9_]*\:[a-z_][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // aliased name
'[a-z_\\\][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // identifier or qualified name
'(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', // numbers
"'(?:[^']|'')*'", // quoted strings
'\?[0-9]*|:[a-z_][a-z0-9_]*', // parameters
];
}
/**
* {@inheritdoc}
*/
protected function getNonCatchablePatterns()
{
return ['\s+', '(.)'];
}
/**
* {@inheritdoc}
*/
protected function getType(&$value)
{
$type = self::T_NONE;
switch (true) {
// Recognize numeric values
case (is_numeric($value)):
if (strpos($value, '.') !== false || stripos($value, 'e') !== false) {
return self::T_FLOAT;
}
return self::T_INTEGER;
// Recognize quoted strings
case ($value[0] === "'"):
$value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));
return self::T_STRING;
// Recognize identifiers, aliased or qualified names
case (ctype_alpha($value[0]) || $value[0] === '_' || $value[0] === '\\'):
$name = 'Doctrine\ORM\Query\Lexer::T_' . strtoupper($value);
if (defined($name)) {
$type = constant($name);
if ($type > 100) {
return $type;
}
}
if (strpos($value, ':') !== false) {
return self::T_ALIASED_NAME;
}
if (strpos($value, '\\') !== false) {
return self::T_FULLY_QUALIFIED_NAME;
}
return self::T_IDENTIFIER;
// Recognize input parameters
case ($value[0] === '?' || $value[0] === ':'):
return self::T_INPUT_PARAMETER;
// Recognize symbols
case ($value === '.'):
return self::T_DOT;
case ($value === ','):
return self::T_COMMA;
case ($value === '('):
return self::T_OPEN_PARENTHESIS;
case ($value === ')'):
return self::T_CLOSE_PARENTHESIS;
case ($value === '='):
return self::T_EQUALS;
case ($value === '>'):
return self::T_GREATER_THAN;
case ($value === '<'):
return self::T_LOWER_THAN;
case ($value === '+'):
return self::T_PLUS;
case ($value === '-'):
return self::T_MINUS;
case ($value === '*'):
return self::T_MULTIPLY;
case ($value === '/'):
return self::T_DIVIDE;
case ($value === '!'):
return self::T_NEGATE;
case ($value === '{'):
return self::T_OPEN_CURLY_BRACE;
case ($value === '}'):
return self::T_CLOSE_CURLY_BRACE;
// Default
default:
// Do nothing
}
return $type;
}
}
This is roughly what the DQL Parser looks like that uses the above
Lexer implementation:
.. note::
You can see the full implementation `here <https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/Parser.php>`_.
.. code-block:: php
class Parser
{
private $lexer;
public function __construct($dql)
{
$this->lexer = new Lexer();
$this->lexer->setInput($dql);
}
// ...
public function getAST()
{
// Parse & build AST
$AST = $this->QueryLanguage();
// ...
return $AST;
}
public function QueryLanguage()
{
$this->lexer->moveNext();
switch ($this->lexer->lookahead['type']) {
case Lexer::T_SELECT:
$statement = $this->SelectStatement();
break;
case Lexer::T_UPDATE:
$statement = $this->UpdateStatement();
break;
case Lexer::T_DELETE:
$statement = $this->DeleteStatement();
break;
default:
$this->syntaxError('SELECT, UPDATE or DELETE');
break;
}
// Check for end of string
if ($this->lexer->lookahead !== null) {
$this->syntaxError('end of string');
}
return $statement;
}
// ...
}
Now the AST is used to transform the DQL query in to portable SQL for whatever relational
database you are using!
.. code-block:: php
$parser = new Parser('SELECT u FROM User u');
$AST = $parser->getAST(); // returns \Doctrine\ORM\Query\AST\SelectStatement
What is an AST?
===============
AST stands for `Abstract syntax tree <http://en.wikipedia.org/wiki/Abstract_syntax_tree>`_.
In computer science, an abstract syntax tree (AST), or just syntax tree, is a
tree representation of the abstract syntactic structure of source code written
in a programming language. Each node of the tree denotes a construct occurring in
the source code.

View File

@@ -1,53 +0,0 @@
Introduction
============
Doctrine Lexer is a library that can be used in Top-Down, Recursive
Descent Parsers. This lexer is used in Doctrine Annotations and in
Doctrine ORM (DQL).
To write your own parser you just need to extend ``Doctrine\Common\Lexer\AbstractLexer``
and implement the following three abstract methods.
.. code-block:: php
/**
* Lexical catchable patterns.
*
* @return array
*/
abstract protected function getCatchablePatterns();
/**
* Lexical non-catchable patterns.
*
* @return array
*/
abstract protected function getNonCatchablePatterns();
/**
* Retrieve token type. Also processes the token value if necessary.
*
* @param string $value
* @return integer
*/
abstract protected function getType(&$value);
These methods define the `lexical <http://en.wikipedia.org/wiki/Lexical_analysis>`_
catchable and non-catchable patterns and a method for returning the
type of a token and filtering the value if necessary.
The Lexer is responsible for giving you an API to walk across a
string one character at a time and analyze the type of each character, value and position of
each token in the string. The low level API of the lexer is pretty simple:
- ``setInput($input)`` - Sets the input data to be tokenized. The Lexer is immediately reset and the new input tokenized.
- ``reset()`` - Resets the lexer.
- ``resetPeek()`` - Resets the peek pointer to 0.
- ``resetPosition($position = 0)`` - Resets the lexer position on the input to the given position.
- ``isNextToken($token)`` - Checks whether a given token matches the current lookahead.
- ``isNextTokenAny(array $tokens)`` - Checks whether any of the given tokens matches the current lookahead.
- ``moveNext()`` - Moves to the next token in the input string.
- ``skipUntil($type)`` - Tells the lexer to skip input tokens until it sees a token with the given value.
- ``isA($value, $token)`` - Checks if given value is identical to the given token.
- ``peek()`` - Moves the lookahead token forward.
- ``glimpse()`` - Peeks at the next token, returns it and immediately resets the peek.

View File

@@ -1,6 +0,0 @@
.. toctree::
:depth: 3
index
simple-parser-example
dql-parser

View File

@@ -1,102 +0,0 @@
Simple Parser Example
=====================
Extend the ``Doctrine\Common\Lexer\AbstractLexer`` class and implement
the ``getCatchablePatterns``, ``getNonCatchablePatterns``, and ``getType``
methods. Here is a very simple example lexer implementation named ``CharacterTypeLexer``.
It tokenizes a string to ``T_UPPER``, ``T_LOWER`` and``T_NUMBER`` tokens:
.. code-block:: php
<?php
use Doctrine\Common\Lexer\AbstractLexer;
class CharacterTypeLexer extends AbstractLexer
{
const T_UPPER = 1;
const T_LOWER = 2;
const T_NUMBER = 3;
protected function getCatchablePatterns()
{
return array(
'[a-bA-Z0-9]',
);
}
protected function getNonCatchablePatterns()
{
return array();
}
protected function getType(&$value)
{
if (is_numeric($value)) {
return self::T_NUMBER;
}
if (strtoupper($value) === $value) {
return self::T_UPPER;
}
if (strtolower($value) === $value) {
return self::T_LOWER;
}
}
}
Use ``CharacterTypeLexer`` to extract an array of upper case characters:
.. code-block:: php
<?php
class UpperCaseCharacterExtracter
{
private $lexer;
public function __construct(CharacterTypeLexer $lexer)
{
$this->lexer = $lexer;
}
public function getUpperCaseCharacters($string)
{
$this->lexer->setInput($string);
$this->lexer->moveNext();
$upperCaseChars = array();
while (true) {
if (!$this->lexer->lookahead) {
break;
}
$this->lexer->moveNext();
if ($this->lexer->token['type'] === CharacterTypeLexer::T_UPPER) {
$upperCaseChars[] = $this->lexer->token['value'];
}
}
return $upperCaseChars;
}
}
$upperCaseCharacterExtractor = new UpperCaseCharacterExtracter(new CharacterTypeLexer());
$upperCaseCharacters = $upperCaseCharacterExtractor->getUpperCaseCharacters('1aBcdEfgHiJ12');
print_r($upperCaseCharacters);
The variable ``$upperCaseCharacters`` contains all of the upper case
characters:
.. code-block:: php
Array
(
[0] => B
[1] => E
[2] => H
[3] => J
)
This is a simple example but it should demonstrate the low level API
that can be used to build more complex parsers.

View File

@@ -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 <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
abstract class AbstractLexer
{
@@ -47,36 +37,43 @@ 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;
/**
* The next token in the input.
*
* @var array
* @var array|null
*/
public $lookahead;
/**
* The last matched/seen token.
*
* @var array
* @var array|null
*/
public $token;
/**
* Composed regex for input parsing.
*
* @var string
*/
private $regex;
/**
* Sets the input data to be tokenized.
*
@@ -90,7 +87,7 @@ abstract class AbstractLexer
public function setInput($input)
{
$this->input = $input;
$this->tokens = array();
$this->tokens = [];
$this->reset();
$this->scan($input);
@@ -104,9 +101,9 @@ abstract class AbstractLexer
public function reset()
{
$this->lookahead = null;
$this->token = null;
$this->peek = 0;
$this->position = 0;
$this->token = null;
$this->peek = 0;
$this->position = 0;
}
/**
@@ -122,7 +119,7 @@ abstract class AbstractLexer
/**
* 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
*/
@@ -134,7 +131,7 @@ abstract class AbstractLexer
/**
* Retrieve the original lexer's input until a given position.
*
* @param integer $position
* @param int $position
*
* @return string
*/
@@ -146,13 +143,13 @@ abstract class AbstractLexer
/**
* 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;
}
/**
@@ -160,23 +157,23 @@ abstract class AbstractLexer
*
* @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;
@@ -199,10 +196,10 @@ abstract class AbstractLexer
/**
* Checks if given value is identical to the given token.
*
* @param mixed $value
* @param integer $token
* @param mixed $value
* @param int|string $token
*
* @return boolean
* @return bool
*/
public function isA($value, $token)
{
@@ -218,9 +215,9 @@ abstract class AbstractLexer
{
if (isset($this->tokens[$this->position + $this->peek])) {
return $this->tokens[$this->position + $this->peek++];
} else {
return null;
}
return null;
}
/**
@@ -230,8 +227,9 @@ abstract class AbstractLexer
*/
public function glimpse()
{
$peek = $this->peek();
$peek = $this->peek();
$this->peek = 0;
return $peek;
}
@@ -244,10 +242,8 @@ abstract class AbstractLexer
*/
protected function scan($input)
{
static $regex;
if ( ! isset($regex)) {
$regex = sprintf(
if (! isset($this->regex)) {
$this->regex = sprintf(
'/(%s)|%s/%s',
implode(')|(', $this->getCatchablePatterns()),
implode('|', $this->getNonCatchablePatterns()),
@@ -255,37 +251,37 @@ abstract class AbstractLexer
);
}
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($regex, $input, -1, $flags);
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($this->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 $token
* @param int|string $token
*
* @return 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) {
@@ -304,7 +300,7 @@ abstract class AbstractLexer
*/
protected function getModifiers()
{
return 'i';
return 'iu';
}
/**
@@ -326,7 +322,7 @@ abstract class AbstractLexer
*
* @param string $value
*
* @return integer
* @return int|string|null
*/
abstract protected function getType(&$value);
}

View File

@@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
convertNoticesToExceptions="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Doctrine lexer Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">lib/Doctrine</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -1,268 +0,0 @@
<?php
namespace Doctrine\Tests\Common\Lexer;
class AbstractLexerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ConcreteLexer
*/
private $concreteLexer;
public function setUp()
{
$this->concreteLexer = new ConcreteLexer();
}
public function dataProvider()
{
return array(
array(
'price=10',
array(
array(
'value' => 'price',
'type' => 'string',
'position' => 0,
),
array(
'value' => '=',
'type' => 'operator',
'position' => 5,
),
array(
'value' => 10,
'type' => 'int',
'position' => 6,
),
),
),
);
}
public function testResetPeek()
{
$expectedTokens = array(
array(
'value' => 'price',
'type' => 'string',
'position' => 0,
),
array(
'value' => '=',
'type' => 'operator',
'position' => 5,
),
array(
'value' => 10,
'type' => 'int',
'position' => 6,
),
);
$this->concreteLexer->setInput('price=10');
$this->assertEquals($expectedTokens[0], $this->concreteLexer->peek());
$this->assertEquals($expectedTokens[1], $this->concreteLexer->peek());
$this->concreteLexer->resetPeek();
$this->assertEquals($expectedTokens[0], $this->concreteLexer->peek());
}
public function testResetPosition()
{
$expectedTokens = array(
array(
'value' => 'price',
'type' => 'string',
'position' => 0,
),
array(
'value' => '=',
'type' => 'operator',
'position' => 5,
),
array(
'value' => 10,
'type' => 'int',
'position' => 6,
),
);
$this->concreteLexer->setInput('price=10');
$this->assertNull($this->concreteLexer->lookahead);
$this->assertTrue($this->concreteLexer->moveNext());
$this->assertEquals($expectedTokens[0], $this->concreteLexer->lookahead);
$this->assertTrue($this->concreteLexer->moveNext());
$this->assertEquals($expectedTokens[1], $this->concreteLexer->lookahead);
$this->concreteLexer->resetPosition(0);
$this->assertTrue($this->concreteLexer->moveNext());
$this->assertEquals($expectedTokens[0], $this->concreteLexer->lookahead);
}
/**
* @dataProvider dataProvider
*
* @param $input
* @param $expectedTokens
*/
public function testMoveNext($input, $expectedTokens)
{
$this->concreteLexer->setInput($input);
$this->assertNull($this->concreteLexer->lookahead);
for ($i = 0; $i < count($expectedTokens); $i++) {
$this->assertTrue($this->concreteLexer->moveNext());
$this->assertEquals($expectedTokens[$i], $this->concreteLexer->lookahead);
}
$this->assertFalse($this->concreteLexer->moveNext());
$this->assertNull($this->concreteLexer->lookahead);
}
public function testSkipUntil()
{
$this->concreteLexer->setInput('price=10');
$this->assertTrue($this->concreteLexer->moveNext());
$this->concreteLexer->skipUntil('operator');
$this->assertEquals(
array(
'value' => '=',
'type' => 'operator',
'position' => 5,
),
$this->concreteLexer->lookahead
);
}
public function testUtf8Mismatch()
{
$this->concreteLexer->setInput("\xE9=10");
$this->assertTrue($this->concreteLexer->moveNext());
$this->assertEquals(
array(
'value' => "\xE9=10",
'type' => 'string',
'position' => 0,
),
$this->concreteLexer->lookahead
);
}
/**
* @dataProvider dataProvider
*
* @param $input
* @param $expectedTokens
*/
public function testPeek($input, $expectedTokens)
{
$this->concreteLexer->setInput($input);
foreach ($expectedTokens as $expectedToken) {
$this->assertEquals($expectedToken, $this->concreteLexer->peek());
}
$this->assertNull($this->concreteLexer->peek());
}
/**
* @dataProvider dataProvider
*
* @param $input
* @param $expectedTokens
*/
public function testGlimpse($input, $expectedTokens)
{
$this->concreteLexer->setInput($input);
foreach ($expectedTokens as $expectedToken) {
$this->assertEquals($expectedToken, $this->concreteLexer->glimpse());
$this->concreteLexer->moveNext();
}
$this->assertNull($this->concreteLexer->peek());
}
public function inputUntilPositionDataProvider()
{
return array(
array('price=10', 5, 'price'),
);
}
/**
* @dataProvider inputUntilPositionDataProvider
*
* @param $input
* @param $position
* @param $expectedInput
*/
public function testGetInputUntilPosition($input, $position, $expectedInput)
{
$this->concreteLexer->setInput($input);
$this->assertSame($expectedInput, $this->concreteLexer->getInputUntilPosition($position));
}
/**
* @dataProvider dataProvider
*
* @param $input
* @param $expectedTokens
*/
public function testIsNextToken($input, $expectedTokens)
{
$this->concreteLexer->setInput($input);
$this->concreteLexer->moveNext();
for ($i = 0; $i < count($expectedTokens); $i++) {
$this->assertTrue($this->concreteLexer->isNextToken($expectedTokens[$i]['type']));
$this->concreteLexer->moveNext();
}
}
/**
* @dataProvider dataProvider
*
* @param $input
* @param $expectedTokens
*/
public function testIsNextTokenAny($input, $expectedTokens)
{
$allTokenTypes = array_map(function ($token) {
return $token['type'];
}, $expectedTokens);
$this->concreteLexer->setInput($input);
$this->concreteLexer->moveNext();
for ($i = 0; $i < count($expectedTokens); $i++) {
$this->assertTrue($this->concreteLexer->isNextTokenAny(array($expectedTokens[$i]['type'])));
$this->assertTrue($this->concreteLexer->isNextTokenAny($allTokenTypes));
$this->concreteLexer->moveNext();
}
}
public function testGetLiteral()
{
$this->assertSame('Doctrine\Tests\Common\Lexer\ConcreteLexer::INT', $this->concreteLexer->getLiteral('int'));
$this->assertSame('fake_token', $this->concreteLexer->getLiteral('fake_token'));
}
public function testIsA()
{
$this->assertTrue($this->concreteLexer->isA(11, 'int'));
$this->assertTrue($this->concreteLexer->isA(1.1, 'int'));
$this->assertTrue($this->concreteLexer->isA('=', 'operator'));
$this->assertTrue($this->concreteLexer->isA('>', 'operator'));
$this->assertTrue($this->concreteLexer->isA('<', 'operator'));
$this->assertTrue($this->concreteLexer->isA('fake_text', 'string'));
}
}

View File

@@ -1,49 +0,0 @@
<?php
namespace Doctrine\Tests\Common\Lexer;
use Doctrine\Common\Lexer\AbstractLexer;
class ConcreteLexer extends AbstractLexer
{
const INT = 'int';
protected function getCatchablePatterns()
{
return array(
'=|<|>',
'[a-z]+',
'\d+',
);
}
protected function getNonCatchablePatterns()
{
return array(
'\s+',
'(.)',
);
}
protected function getType(&$value)
{
if (is_numeric($value)) {
$value = (int)$value;
return 'int';
}
if (in_array($value, array('=', '<', '>'))) {
return 'operator';
}
if (is_string($value)) {
return 'string';
}
return;
}
protected function getModifiers()
{
return parent::getModifiers().'u';
}
}