Issues (35)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Parser/Parser.php (14 issues)

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
3
declare(strict_types=1);
4
5
namespace RulerZ\Parser;
6
7
use Hoa\Compiler;
8
use Hoa\File;
9
use Hoa\Ruler;
10
use Hoa\Visitor;
11
12
use RulerZ\Model;
13
14
/**
15
 * Parses a rule.
16
 *
17
 * A valid rule returns an AST:
18
 * ```
19
 * $parser = new Parser;
20
 * $ast = $parser->parse('foo = 42');
21
 * ```
22
 *
23
 * And an invalid one throw an exception:
24
 * ```should_throw
25
 * $parser = new Parser;
26
 * $parser->parse('foo = ');
27
 * ```
28
 */
29
class Parser implements Visitor\Visit
30
{
31
    /**
32
     * Parser.
33
     *
34
     * @var \Hoa\Compiler\Llk\Parser
35
     */
36
    private $parser;
37
38
    /**
39
     * Root.
40
     *
41
     * @var \RulerZ\Model\Rule object
42
     */
43
    private $root;
44
45
    /**
46
     * Next positional parameter index.
47
     *
48
     * @var int
49
     */
50
    private $nextParameterIndex = 0;
51
52
    /**
53
     * Parses the rule into an equivalent AST.
54
     *
55
     * @param string $rule The rule represented as a string.
56
     *
57
     * @return \RulerZ\Model\Rule
58
     */
59
    public function parse($rule)
60
    {
61
        if ($this->parser === null) {
62
            $this->parser = Compiler\Llk::load(
63
                new File\Read(__DIR__.'/../Grammar.pp')
64
            );
65
        }
66
67
        $this->nextParameterIndex = 0;
68
69
        return $this->visit($this->parser->parse($rule));
70
    }
71
72
    /**
73
     * Visit an element.
74
     *
75
     * @param   \Hoa\Visitor\Element  $element    Element to visit.
76
     * @param   mixed                 &$handle    Handle (reference).
77
     * @param   mixed                 $eldnah     Handle (not reference).
78
     *
79
     * @return  \RulerZ\Model\Rule
80
     *
81
     * @throws  \Hoa\Ruler\Exception\Interpreter
82
     */
83
    public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
84
    {
85
        /** @var \Hoa\Compiler\Llk\TreeNode $element */
86
        $id = $element->getId();
87
        $variable = false !== $eldnah;
88
89
        switch ($id) {
90
            case '#expression':
91
                $this->root = new Model\Rule();
92
                $this->root->expression = $element->getChild(0)->accept(
0 ignored issues
show
The property expression does not exist on object<RulerZ\Model\Rule>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
                    $this,
94
                    $handle,
95
                    $eldnah
96
                );
97
98
                return $this->root;
99
100
            case '#operation':
101
                $children = $element->getChildren();
102
                $left = $children[0]->accept($this, $handle, $eldnah);
103
                $right = $children[2]->accept($this, $handle, $eldnah);
104
                $name = $children[1]->accept($this, $handle, false);
105
106
                return $this->root->_operator(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->root->_ope...$left, $right), false); (Hoa\Ruler\Model\Operator) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
                    $name,
108
                    [$left, $right],
109
                    false
110
                );
111
112
            case '#variable_access':
113
                $children = $element->getChildren();
114
                $name = $children[0]->accept($this, $handle, $eldnah);
115
                array_shift($children);
116
117
                foreach ($children as $child) {
118
                    $_child = $child->accept($this, $handle, $eldnah);
119
120
                    switch ($child->getId()) {
121
                        case '#attribute_access':
122
                            $name->attribute($_child);
123
124
                            break;
125
                    }
126
                }
127
128
                return $name;
129
130
            case '#attribute_access':
131
                return $element->getChild(0)->accept($this, $handle, false);
132
133
            case '#array_declaration':
134
                $out = [];
135
136
                foreach ($element->getChildren() as $child) {
137
                    $out[] = $child->accept($this, $handle, $eldnah);
138
                }
139
140
                return $out;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $out; (array) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
141
142
            case '#function_call':
143
                $children = $element->getChildren();
144
                $name = $children[0]->accept($this, $handle, false);
145
                array_shift($children);
146
147
                $arguments = [];
148
149
                foreach ($children as $child) {
150
                    $arguments[] = $child->accept($this, $handle, $eldnah);
151
                }
152
153
                return $this->root->_operator(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->root->_ope...ame, $arguments, true); (Hoa\Ruler\Model\Operator) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
154
                    $name,
155
                    $arguments,
156
                    true
157
                );
158
159
            case '#and':
160
            case '#or':
161
            case '#xor':
162
                $name = substr($id, 1);
163
                $children = $element->getChildren();
164
                $left = $children[0]->accept($this, $handle, $eldnah);
165
                $right = $children[1]->accept($this, $handle, $eldnah);
166
167
                return $this->root->operation($name, [$left, $right]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->root->oper... array($left, $right)); (Hoa\Ruler\Model\Operator) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
168
169
            case '#not':
170
                return $this->root->operation(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->root->oper...s, $handle, $eldnah))); (Hoa\Ruler\Model\Operator) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
171
                    'not',
172
                    [$element->getChild(0)->accept($this, $handle, $eldnah)]
173
                );
174
175
            case 'token':
176
                $token = $element->getValueToken();
177
                $value = $element->getValueValue();
178
179
                switch ($token) {
180
                    case 'identifier':
181
                        return true === $variable ? $this->root->variable($value) : $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true === $variabl...iable($value) : $value; (Hoa\Ruler\Model\Bag\Context|string) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
182
183
                    case 'named_parameter':
184
185
                        return new Model\Parameter(substr($value, 1));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \RulerZ\Model...ter(substr($value, 1)); (RulerZ\Model\Parameter) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
186
187
                    case 'positional_parameter':
188
                        $index = $this->nextParameterIndex++;
189
190
                        return new Model\Parameter($index);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \RulerZ\Model\Parameter($index); (RulerZ\Model\Parameter) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
191
192
                    case 'true':
193
                        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
194
195
                    case 'false':
196
                        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
197
198
                    case 'null':
199
                        return null;
200
201
                    case 'float':
202
                        return (float) $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (double) $value; (double) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
203
204
                    case 'integer':
205
                        return (int) $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (int) $value; (integer) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
206
207
                    case 'string':
208
                        return str_replace(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return str_replace('\\' ...substr($value, 1, -1)); (string) is incompatible with the return type documented by RulerZ\Parser\Parser::visit of type RulerZ\Model\Rule.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
209
                            '\\'.$value[0],
210
                            $value[0],
211
                            substr($value, 1, -1)
212
                        );
213
214
                    default:
215
                        throw new Ruler\Exception\Interpreter('Token %s is unknown.', 0, $token);
216
                }
217
218
            default:
219
                throw new Ruler\Exception\Interpreter('Element %s is unknown.', 1, $id);
220
        }
221
    }
222
}
223