Merge branch '2.7' into 2.8

* 2.7:
  don't keep internal state between parser runs
  Add \Traversable typehint to phpdoc
  [ExpressionLanguage] Avoid dependency on ctype
  [Debug] Fix php notice
This commit is contained in:
Christian Flothmann 2017-04-09 20:13:48 +02:00
commit b89ed60442
5 changed files with 63 additions and 22 deletions

View File

@ -186,7 +186,7 @@ class DebugClassLoader
$exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
if ('\\' === $class[0]) {
if ($class && '\\' === $class[0]) {
$class = substr($class, 1);
}

View File

@ -45,7 +45,7 @@ class Lexer
if (preg_match('/[0-9]+(?:\.[0-9]+)?/A', $expression, $match, null, $cursor)) {
// numbers
$number = (float) $match[0]; // floats
if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) {
if (preg_match('/^[0-9]+$/', $match[0]) && $number <= PHP_INT_MAX) {
$number = (int) $match[0]; // integers lower than the maximum
}
$tokens[] = new Token(Token::NUMBER_TYPE, $number, $cursor + 1);

View File

@ -19,8 +19,8 @@ interface DataMapperInterface
/**
* Maps properties of some data to a list of forms.
*
* @param mixed $data Structured data
* @param FormInterface[] $forms A list of {@link FormInterface} instances
* @param mixed $data Structured data
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
*
* @throws Exception\UnexpectedTypeException if the type of the data parameter is not supported.
*/
@ -29,8 +29,8 @@ interface DataMapperInterface
/**
* Maps the data of a list of forms into the properties of some data.
*
* @param FormInterface[] $forms A list of {@link FormInterface} instances
* @param mixed $data Structured data
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
* @param mixed $data Structured data
*
* @throws Exception\UnexpectedTypeException if the type of the data parameter is not supported.
*/

View File

@ -64,23 +64,51 @@ class Parser
if (false === preg_match('//u', $value)) {
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
}
$this->currentLineNb = -1;
$this->currentLine = '';
$value = $this->cleanup($value);
$this->lines = explode("\n", $value);
if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = count($this->lines);
}
$this->refs = array();
$mbEncoding = null;
$e = null;
$data = null;
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
}
try {
$data = $this->doParse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
} catch (\Exception $e) {
} catch (\Throwable $e) {
}
if (null !== $mbEncoding) {
mb_internal_encoding($mbEncoding);
}
if (null !== $e) {
throw $e;
}
return $data;
}
private function doParse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
{
$this->currentLineNb = -1;
$this->currentLine = '';
$value = $this->cleanup($value);
$this->lines = explode("\n", $value);
$this->locallySkippedLineNumbers = array();
if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = count($this->lines);
}
$data = array();
$context = null;
$allowOverwrite = false;
while ($this->moveToNextLine()) {
if ($this->isCurrentLineEmpty()) {
continue;
@ -246,10 +274,6 @@ class Parser
throw $e;
}
if (isset($mbEncoding)) {
mb_internal_encoding($mbEncoding);
}
return $value;
}
@ -257,10 +281,6 @@ class Parser
}
}
if (isset($mbEncoding)) {
mb_internal_encoding($mbEncoding);
}
if ($objectForMap && !is_object($data) && 'mapping' === $context) {
$object = new \stdClass();
@ -289,7 +309,7 @@ class Parser
$parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
$parser->refs = &$this->refs;
return $parser->parse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
return $parser->doParse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
}
/**

View File

@ -1231,6 +1231,27 @@ YAML
$this->assertEquals($trickyVal, $arrayFromYaml);
}
/**
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
* @expectedExceptionMessage Reference "foo" does not exist at line 2
*/
public function testParserCleansUpReferencesBetweenRuns()
{
$yaml = <<<YAML
foo: &foo
baz: foobar
bar:
<<: *foo
YAML;
$this->parser->parse($yaml);
$yaml = <<<YAML
bar:
<<: *foo
YAML;
$this->parser->parse($yaml);
}
}
class B