Skip to content

Commit 505d1c7

Browse files
committed
Isolate bracket array item key extraction
1 parent 3faa4fd commit 505d1c7

File tree

2 files changed

+64
-27
lines changed

2 files changed

+64
-27
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace JsPhpize\Parser;
4+
5+
use JsPhpize\Lexer\Token;
6+
7+
class BracketsArrayItemKey
8+
{
9+
/**
10+
* @var Token
11+
*/
12+
protected $token;
13+
14+
/**
15+
* @var array
16+
*/
17+
protected $typeAndValue;
18+
19+
public function __construct(Token $token)
20+
{
21+
$this->token = $token;
22+
$this->typeAndValue = $this->parseTypeAndValue();
23+
}
24+
25+
protected function getStringExport($value)
26+
{
27+
return array('string', var_export($value, true));
28+
}
29+
30+
protected function parseTypeAndValue()
31+
{
32+
$token = $this->token;
33+
34+
if ($token->is('keyword')) {
35+
return $this->getStringExport($token->value);
36+
}
37+
38+
if ($token->isValue()) {
39+
$type = $token->type;
40+
$value = $token->value;
41+
42+
if ($type === 'variable') {
43+
return $this->getStringExport($value);
44+
}
45+
46+
return array($token->type, $token->value);
47+
}
48+
49+
return array(null, null);
50+
}
51+
52+
public function isValid()
53+
{
54+
return !is_null($this->typeAndValue[0]);
55+
}
56+
57+
public function get()
58+
{
59+
return $this->typeAndValue;
60+
}
61+
}

src/JsPhpize/Parser/TokenExtractor.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,19 @@
22

33
namespace JsPhpize\Parser;
44

5-
use JsPhpize\Lexer\Token;
65
use JsPhpize\Nodes\Assignation;
76
use JsPhpize\Nodes\Constant;
87
use JsPhpize\Nodes\Dyiade;
98
use JsPhpize\Nodes\FunctionCall;
109

1110
abstract class TokenExtractor extends TokenCrawler
1211
{
13-
protected function getStringExport($value)
14-
{
15-
return array('string', var_export($value, true));
16-
}
17-
18-
protected function getTypeAndValueFromToken(Token $token)
19-
{
20-
if ($token->is('keyword')) {
21-
return $this->getStringExport($token->value);
22-
}
23-
24-
if ($token->isValue()) {
25-
$type = $token->type;
26-
$value = $token->value;
27-
28-
if ($type === 'variable') {
29-
return $this->getStringExport($value);
30-
}
31-
32-
return array($token->type, $token->value);
33-
}
34-
35-
return array(null, null);
36-
}
3712
protected function getBracketsArrayItemKeyFromToken($token)
3813
{
39-
list($type, $value) = $this->getTypeAndValueFromToken($token);
14+
$typeAndValue = new BracketsArrayItemKey($token);
4015

41-
if ($type) {
16+
if ($typeAndValue->isValid()) {
17+
list($type, $value) = $typeAndValue->get();
4218
$token = $this->next();
4319
if (!$token) {
4420
throw new Exception('Missing value after ' . $value . $this->exceptionInfos(), 12);

0 commit comments

Comments
 (0)