Giter Club home page Giter Club logo

phptokenizer's People

Contributors

chrisgalliano avatar dependabot[bot] avatar funivan avatar jiripudil avatar scrutinizer-auto-fixer avatar sergeimv avatar ttacoder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

phptokenizer's Issues

Get class FQN

If class is declared/used inside namespace then get full class name

Array modification

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:)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.