funivan / phptokenizer Goto Github PK
View Code? Open in Web Editor NEWWrapper around token_get_all. Easy way to extract and modify yout code
License: MIT License
Wrapper around token_get_all. Easy way to extract and modify yout code
License: MIT License
Is it possible to remove one specific function (by its name) from source with PhpTokenizer?
https://github.com/funivan/PhpTokenizer/blob/master/src/QuerySequence/QuerySequence.php#L107 try to change from search
to strict
If class is declared/used inside namespace then get full class name
public function equals(Token $token) {
# check type and value
}
By default try to find all items inside collection.
Hi there,
First off, great library:)
I need to parse a file and modify the containing array. I've got this far:
class ArrayFormatter
{
/**
* @var Collection
*/
protected $collection;
/**
* @param $code
*/
public function __construct($code)
{
$this->collection = Collection::createFromString($code);
}
/**
* @param $key
* @param $items
*/
public function append($key, $items)
{
$q = new QuerySequence($this->collection);
$start = $q->search("'" . $key . "'");
$q->setPosition($start->getIndex());
$end = $q->section('[', ']');
$position = $start->getIndex() + $end->count() + 1;
$tokens = $this->renderItemTokens($items);
if ($q->isValid()) {
// select the last first token after the opening bracket
// we may need to add a comma separator if the array isn't empty
$comma = $this->collection->extractItems($position, 1);
if (!in_array($comma[0]->getValue(), [',', '['])) {
array_unshift($tokens, new Token([-1, ',', 0, $position]));
}
$this->collection->addAfter($position, $tokens);
}
}
/**
* @return string
*/
public function toString()
{
return (string)$this->collection;
}
protected function renderItemTokens($items)
{
$tokens = [];
$isNumeric = $this->isNumeric($items);
foreach ($items as $key => $item) {
$tokens[] = new Token([T_WHITESPACE, "\n" . str_repeat(' ', 8), 0, 0]);
if (!$isNumeric) {
$tokens[] = new Token([T_CONSTANT_ENCAPSED_STRING, "'" . $key . "'", 0, 0]);
$tokens[] = new Token([T_WHITESPACE, ' ', 0, 0]);
$tokens[] = new Token([T_DOUBLE_ARROW, '=>', 0, 0]);
$tokens[] = new Token([T_WHITESPACE, ' ', 0, 0]);
}
$tokens[] = new Token([T_CONSTANT_ENCAPSED_STRING, $item, 0, 0]);
$tokens[] = new Token([-1, ',', 0, 0]);
}
return $tokens;
}
protected function isNumeric($items)
{
return isset($items[count($items) - 1]);
}
}
My test code as follows:
$source = "<?php return [
'key1' => [
'test1',
'key' => 'key2'
],
'key2' => [
'asd'
]
];";
$array = new ArrayFormatter($source);
$array->append('key2', [
'key1' => 'DummyValue1',
'key2' => 'DummyValue1',
'key3' => 'DummyValue1',
]);
$array->append('key1', [
'TestValue1',
'TestValue2',
'TestValue3'
]);
echo $array->toString(), PHP_EOL;
The problem is that the new array items don't get added to the correct positions.
Result:
<?php return [
'key1' => [
'test1',
'key' => 'key2',
TestValue1,
TestValue2,
TestValue3,
],
'key2' ,
'key1' => DummyValue1,
'key2' => DummyValue1,
'key3' => DummyValue1,=> [
'asd'
]
];
Also if I change the order of the ->append() calls, I get the following output:
<?php return [
'key1' => [
'test1',
'key' => 'key2',
TestValue1,
TestValue2,
'key1' => DummyValue1,
'key2' => DummyValue1,
'key3' => DummyValue1,,
TestValue3,
],
'key2' => [
'asd'
]
];
I think I'm doing something wrong with the query object, but can't figure out where the problem is.
Any help is greatly appreciated:)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.