Completed
Push — master ( a62027...79a6e0 )
by Maxim
03:54 queued 01:49
created

ConditionsProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processInputData() 0 3 1
A processOutputData() 0 3 1
B makePhpConditions() 0 41 2
1
<?php
2
/**
3
 * This file is a part of "Axessors" library.
4
 *
5
 * @author <[email protected]>
6
 * @license GPL
7
 */
8
9
namespace NoOne4rever\Axessors;
10
11
/**
12
 * Class ConditionsProcessor.
13
 *
14
 * Processes Axessors conditions.
15
 *
16
 * @package NoOne4rever\Axessors
17
 */
18
class ConditionsProcessor extends TokenProcessor
19
{
20
    /**
21
     * Creates list of conditions for input data.
22
     *
23
     * @return string conditions
24
     */
25 2
    public function processInputData(): string
26
    {
27 2
        return $this->makePhpConditions($this->input);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->makePhpConditions($this->input) returns the type string which is incompatible with the return type mandated by NoOne4rever\Axessors\Tok...sor::processInputData() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
28
    }
29
30
    /**
31
     * Creates list of conditions for output data.
32
     *
33
     * @return string conditions
34
     */
35 2
    public function processOutputData(): string
36
    {
37 2
        return $this->makePhpConditions($this->output);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->makePhpConditions($this->output) returns the type string which is incompatible with the return type mandated by NoOne4rever\Axessors\Tok...or::processOutputData() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
38
    }
39
40
    /**
41
     * Turns Axessors conditions into PHP code.
42
     *
43
     * @param string $axsConditions Axessors conditional statement
44
     *
45
     * @return string
46
     */
47 2
    private function makePhpConditions(string $axsConditions): string
48
    {
49 2
        if ($axsConditions === '') {
50 2
            return 'true';
51
        }
52 2
        $injProcessor = new InjectedStringSuit($axsConditions);
53 2
        $axsConditions = $injProcessor->resolveClassNames($this->namespace)->processThis()->wrapWithClosure()
54 2
            ->addSlashes('<>!=')
55 2
            ->get();
56 2
        $axsConditions = preg_replace_callback(
57 2
            '/(((>|<)=?)|(=|!)=)\s*\d+/',
58 2
            function (array $matches): string {
59 2
                return sprintf('\%s::count($var) %s', ConditionsRunner::class, $matches[0]);
60 2
            },
61 2
            $axsConditions
62
        );
63 2
        $axsConditions = preg_replace_callback(
64 2
            '/\d+\.\.\d+/',
65 2
            function (array $matches): string {
66 2
                list($min, $max) = explode('..', $matches[0]);
67 2
                return sprintf('\%s::count($var) >= %d'
68 2
                    . ' && \%s::count($var) <= %d', ConditionsRunner::class, $min, ConditionsRunner::class, $max);
69 2
            },
70 2
            $axsConditions
71
        );
72 2
        $axsConditions = preg_replace_callback(
73 2
            '/\\\\[<>!=]/',
74 2
            function (array $matches): string {
75 2
                return substr($matches[0], 1);
76 2
            },
77 2
            $axsConditions
78
        );
79 2
        $axsConditions = preg_replace_callback(
80 2
            '/`[^`]+`/',
81 2
            function (array $matches): string {
82 2
                $subject = substr($matches[0], 1, strlen($matches[0]) - 2);
83 2
                return sprintf('(function($var){return %s;})($var)', $subject);
84 2
            },
85
            $axsConditions
86
        );
87
        return $axsConditions;
88
    }
89
}