Skip to content

Commit f82fa89

Browse files
committed
Add explicit array checking for peeks, enhance the checks for variables.
1 parent 53cf29c commit f82fa89

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ A small library to parse text representations of a PHP array and return an actua
33

44
## Installation
55

6-
Run `composer install` to run this script (and tests) in a standalone way. Alternatively, this can be used as a dependency in another project by running `composer require battye/php-array-parser:1.0.4`.
6+
Run `composer install` to run this script (and tests) in a standalone way. Alternatively, this can be used as a dependency in another project by running `composer require battye/php-array-parser "~1.0"`.
77

88
If you notice any bugs, please feel free to raise an issue or pull request.
99

src/parser.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,22 @@ public function parse_value()
7373
// Ignore values that rely on another variable
7474
if (!$this->tokens->done())
7575
{
76-
if (count($this->tokens->peek()) > 1 && substr($this->tokens->peek()[1], 0, 1) === "$")
76+
if (is_array($this->tokens->peek()) && count($this->tokens->peek()) > 1 && substr($this->tokens->peek()[1], 0, 1) === "$")
7777
{
78+
// If a variable is being used as a value then don't bother with this any more
79+
$literalValue = self::NOT_STRING_LITERAL;
80+
7881
while (!$this->tokens->does_match(","))
7982
{
83+
if ($this->tokens->does_match(")"))
84+
{
85+
return $literalValue;
86+
}
87+
8088
$this->tokens->pop(); // Ignore comments
8189
}
8290

83-
// If a variable is being used as a value then don't bother with this any more
84-
return self::NOT_STRING_LITERAL;
91+
return $literalValue;
8592
}
8693
}
8794

@@ -145,7 +152,7 @@ public function ignore_comments()
145152
if (!$this->tokens->done())
146153
{
147154
// Check for both short and long form comment blocks
148-
while (count($this->tokens->peek()) > 1 && $this->is_comment_block($this->tokens->peek()[1]))
155+
while (is_array($this->tokens->peek()) && count($this->tokens->peek()) > 1 && $this->is_comment_block($this->tokens->peek()[1]))
149156
{
150157
$this->tokens->pop(); // Ignore comments
151158
}

src/tokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function does_match($what)
6262
{
6363
$token = $this->peek();
6464

65-
if (is_string($what) && ! is_array($token) && $token === $what)
65+
if (is_string($what) && !is_array($token) && $token === $what)
6666
{
6767
return true;
6868
}

tests/ArrayParserTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ public function testCanParseSimpleString()
3030
$this->assertArraySubset($expected_simple_output, $result);
3131
}
3232

33+
/**
34+
* Test that variables can be picked up
35+
*/
36+
public function testCanDetectVariable()
37+
{
38+
// Put some variable names in there; test that we can have them in the start, middle or end
39+
$string = "array(0 => array('one' => \$test, 'two' => \$test, 'three' => \$test));";
40+
41+
$tokens = new tokens($string);
42+
$parser = new parser($tokens);
43+
$result = $parser->parse_array();
44+
45+
// Expected output
46+
$expected_simple_output = array(
47+
0 => array(
48+
'one' => parser::NOT_STRING_LITERAL,
49+
'two' => parser::NOT_STRING_LITERAL,
50+
'three' => parser::NOT_STRING_LITERAL,
51+
)
52+
);
53+
54+
$this->assertArraySubset($expected_simple_output, $result);
55+
}
56+
3357
/**
3458
* Test that an event file is parsed correctly
3559
*/

0 commit comments

Comments
 (0)