Completed
Push — master ( b4b898...f81b53 )
by Woody
11s
created

src/Ruler/Visitor.php (5 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder\Ruler;
5
6
use Hoa\Ruler\Model as Ast;
7
use Hoa\Ruler\Visitor\Disassembly;
8
use Hoa\Visitor\Element as ElementInterface;
9
use Hoa\Visitor\Visit as VisitorInterface;
10
11
use function Latitude\QueryBuilder\criteria;
12
use function Latitude\QueryBuilder\express;
13
use function Latitude\QueryBuilder\identify;
14
use function Latitude\QueryBuilder\listing;
15
use function Latitude\QueryBuilder\literal;
16
use function Latitude\QueryBuilder\param;
17
18
class Visitor implements VisitorInterface
19
{
20
    const SUPPORTED_ACCESS_TYPE = [
21
        Ast\Bag\Context::ARRAY_ACCESS,
22
        Ast\Bag\Context::ATTRIBUTE_ACCESS,
23
    ];
24
25
    /** @var Disassembly */
26
    protected $disassembly;
27
28 6
    public function __construct()
29
    {
30 6
        $this->disassembly = new Disassembly();
31 6
    }
32
33 6
    public function visit(ElementInterface $element, &$handle = null, $eldnah = null)
34
    {
35 6
        if ($element instanceof Ast\Model) {
0 ignored issues
show
The class Hoa\Ruler\Model\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
36 6
            return $this->visitModel($element, $handle, $eldnah);
37
        }
38
39 6
        if ($element instanceof Ast\Operator) {
0 ignored issues
show
The class Hoa\Ruler\Model\Operator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
40 6
            return $this->visitOperator($element, $handle, $eldnah);
41
        }
42
43 6
        if ($element instanceof Ast\Bag\Scalar) {
0 ignored issues
show
The class Hoa\Ruler\Model\Bag\Scalar does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
44 4
            return $this->visitScalar($element, $handle, $eldnah);
45
        }
46
47 6
        if ($element instanceof Ast\Bag\RulerArray) {
0 ignored issues
show
The class Hoa\Ruler\Model\Bag\RulerArray does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
48 2
            return $this->visitArray($element, $handle, $eldnah);
49
        }
50
51 6
        if ($element instanceof Ast\Bag\Context) {
0 ignored issues
show
The class Hoa\Ruler\Model\Bag\Context does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
52 6
            return $this->visitAccess($element, $handle, $eldnah);
53
        }
54
55
        // @codeCoverageIgnoreStart
56
        throw new \LogicException(sprintf('Element of type "%s" not handled', get_class($element)));
57
        // @codeCoverageIgnoreEnd
58
    }
59
60 6
    protected function visitAccess(Ast\Bag\Context $element, &$handle = null, $eldnah = null)
61
    {
62 6
        if ($element->getId() === '?') {
63 1
            return literal($element->getId());
64
        }
65
66 6
        $identifier = [$element->getId()];
67 6
        foreach ($element->getDimensions() as $dimension) {
68 4
            $this->assertValidAccess($element, $dimension[Ast\Bag\Context::ACCESS_TYPE]);
69 3
            $identifier[] = $dimension[Ast\Bag\Context::ACCESS_VALUE];
70
        }
71
72 5
        return identify(implode('.', $identifier));
73
    }
74
75 4
    protected function assertValidAccess(Ast\Bag\Context $element, int $type): void
76
    {
77 4
        if (in_array($type, self::SUPPORTED_ACCESS_TYPE) === false) {
78 1
            throw new \LogicException(sprintf(
79 1
                'Invalid access type in expression: %s',
80 1
                $this->disassembly->visit($element)
81
            ));
82
        }
83 3
    }
84
85 4
    protected function visitScalar(Ast\Bag\Scalar $element, &$handle = null, $eldnah = null)
86
    {
87 4
        return param($element->getValue());
88
    }
89
90 2
    protected function visitArray(Ast\Bag\RulerArray $element, &$handle = null, $eldnah = null)
91
    {
92 2
        return express('(%s)', listing(array_map($this->remapper($handle, $eldnah), $element->getArray())));
93
    }
94
95 6
    protected function visitModel(Ast\Model $element, &$handle = null, $eldnah = null)
96
    {
97 6
        return $element->getExpression()->accept($this, $handle, $eldnah);
98
    }
99
100 6
    protected function visitOperator(Ast\Operator $element, &$handle = null, $eldnah = null)
101
    {
102 6
        $values = array_map($this->remapper($handle, $eldnah), $element->getArguments());
103
104 5
        if ($element->isFunction()) {
105 1
            return criteria(sprintf('%s(%%s)', strtoupper($element->getName())), listing($values));
106
        }
107
108 5
        if (count($values) === 2) {
109 5
            return criteria(sprintf('%%s %s %%s', $element->getName()), $values[0], $values[1]);
110
        }
111
112 1
        return criteria(sprintf('%s (%%s)', $element->getName()), listing($values, ' '));
113
    }
114
115
    protected function remapper(&$handle, $eldnah): callable
116
    {
117 6
        return function ($element) use (&$handle, $eldnah) {
118 6
            return $element->accept($this, $handle, $eldnah);
119 6
        };
120
    }
121
}
122