Issues (54)

src/Rule.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
/**
4
 * @license     http://opensource.org/licenses/mit-license.php MIT
5
 * @link        https://github.com/nicoSWD
6
 * @author      Nicolas Oelgart <[email protected]>
7
 */
8
namespace nicoSWD\Rule;
9
10
use Exception;
11
use nicoSWD\Rule\Evaluator\EvaluatorInterface;
12
13
class Rule
14
{
15
    private string $rule;
16
    private Parser\Parser $parser;
0 ignored issues
show
The type nicoSWD\Rule\Parser\Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
    private string $parsedRule = '';
18
    private string $error = '';
19
    private static object $container;
20
21
    public function __construct(string $rule, array $variables = [])
22
    {
23
        if (!isset(self::$container)) {
24
            self::$container = require __DIR__ . '/container.php';
25
        }
26 280
27
        $this->parser = self::$container->parser($variables);
28 280
        $this->rule = $rule;
29 2
    }
30
31
    public function isTrue(): bool
32 280
    {
33 280
        /** @var EvaluatorInterface $evaluator */
34 280
        $evaluator = self::$container->evaluator();
35
36 212
        return $evaluator->evaluate(
37
            $this->parsedRule ?:
38
            $this->parser->parse($this->rule)
39 212
        );
40
    }
41 212
42 212
    public function isFalse(): bool
43 212
    {
44
        return !$this->isTrue();
45
    }
46
47 2
    /**
48
     * Tells whether a rule is valid (as in "can be parsed without error") or not.
49 2
     */
50
    public function isValid(): bool
51
    {
52
        try {
53
            $this->parsedRule = $this->parser->parse($this->rule);
54
        } catch (Exception $e) {
55 74
            $this->error = $e->getMessage();
56
            return false;
57
        }
58 74
59 68
        return true;
60 68
    }
61 68
62
    public function getError(): string
63
    {
64 6
        return $this->error;
65
    }
66
}
67