Completed
Push — master ( d6297f...1e5c60 )
by Jean
03:33
created

AbstractRule::dump()   C

Complexity

Conditions 15
Paths 75

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 16.6967

Importance

Changes 0
Metric Value
cc 15
nc 75
nop 2
dl 0
loc 71
ccs 41
cts 51
cp 0.8038
crap 16.6967
rs 5.9166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace JClaveau\LogicalFilter\Rule;
3
4
abstract class AbstractRule implements \JsonSerializable
5
{
6
    use Trait_RuleWithOptions;
7
    use Trait_RuleWithCache;
8
    use Trait_ExportableRule;
9
    use Trait_RuleFactory;
10
11
    /**
12
     * Clones the rule with a chained syntax.
13
     *
14
     * @return AbstractRule A copy of the current instance.
15
     */
16 135
    public function copy()
17
    {
18 135
        return clone $this;
19
    }
20
21
    /**
22
     * @deprecated addMinimalCase
23
     */
24 4
    protected function forceLogicalCore()
25
    {
26 4
        return $this->addMinimalCase();
27
    }
28
29
    /**
30
     * Forces the two firsts levels of the tree to be an OrRule having
31
     * only AndRules as operands:
32
     * ['field', '=', '1'] <=> ['or', ['and', ['field', '=', '1']]]
33
     * As a simplified ruleTree will alwways be reduced to this structure
34
     * with no suboperands others than atomic ones or a simpler one like:
35
     * ['or', ['field', '=', '1'], ['field2', '>', '3']]
36
     *
37
     * This helpes to ease the result of simplify()
38
     *
39
     * @return OrRule
40
     */
41 48
    public function addMinimalCase()
42
    {
43
        // Simplification step is required to call hasSolution() on the
44
        // returned OrRule value
45 48
        if ($this instanceof AndRule || $this instanceof OrRule) {
46 33
            $simplification_step_to_keep = $this->getSimplificationStep();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JClaveau\LogicalFilter\Rule\AbstractRule as the method getSimplificationStep() does only exist in the following sub-classes of JClaveau\LogicalFilter\Rule\AbstractRule: JClaveau\LogicalFilter\Rule\AboveOrEqualRule, JClaveau\LogicalFilter\Rule\AbstractOperationRule, JClaveau\LogicalFilter\Rule\AndRule, JClaveau\LogicalFilter\Rule\BelowOrEqualRule, JClaveau\LogicalFilter\Rule\BetweenOrEqualBothRule, JClaveau\LogicalFilter\R...BetweenOrEqualLowerRule, JClaveau\LogicalFilter\R...BetweenOrEqualUpperRule, JClaveau\LogicalFilter\Rule\BetweenRule, JClaveau\LogicalFilter\Rule\InRule, JClaveau\LogicalFilter\Rule\NotEqualRule, JClaveau\LogicalFilter\Rule\NotInRule, JClaveau\LogicalFilter\Rule\NotRule, JClaveau\LogicalFilter\Rule\OrRule. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
47 33
        }
48 19
        elseif ($this->hasSolution()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JClaveau\LogicalFilter\Rule\AbstractRule as the method hasSolution() does only exist in the following sub-classes of JClaveau\LogicalFilter\Rule\AbstractRule: JClaveau\LogicalFilter\Rule\AboveOrEqualRule, JClaveau\LogicalFilter\Rule\AboveRule, JClaveau\LogicalFilter\Rule\AndRule, JClaveau\LogicalFilter\Rule\BelowOrEqualRule, JClaveau\LogicalFilter\Rule\BelowRule, JClaveau\LogicalFilter\Rule\BetweenOrEqualBothRule, JClaveau\LogicalFilter\R...BetweenOrEqualLowerRule, JClaveau\LogicalFilter\R...BetweenOrEqualUpperRule, JClaveau\LogicalFilter\Rule\BetweenRule, JClaveau\LogicalFilter\Rule\EqualRule, JClaveau\LogicalFilter\Rule\InRule, JClaveau\LogicalFilter\Rule\NotEqualRule, JClaveau\LogicalFilter\Rule\NotInRule, JClaveau\LogicalFilter\Rule\NotRule, JClaveau\LogicalFilter\Rule\OrRule, JClaveau\LogicalFilter\Rule\RegexpRule. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
49 18
            $simplification_step_to_keep = AbstractOperationRule::simplified;
50 18
        }
51
        else {
52 1
            $simplification_step_to_keep = null;
53
        }
54
55
        if (   $this instanceof AbstractAtomicRule
56 48
            || $this instanceof NotRule
57 37
            || $this instanceof InRule
58 33
            || ! $this->isNormalizationAllowed([])
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JClaveau\LogicalFilter\Rule\AbstractRule as the method isNormalizationAllowed() does only exist in the following sub-classes of JClaveau\LogicalFilter\Rule\AbstractRule: JClaveau\LogicalFilter\Rule\AboveOrEqualRule, JClaveau\LogicalFilter\Rule\AbstractOperationRule, JClaveau\LogicalFilter\Rule\AndRule, JClaveau\LogicalFilter\Rule\BelowOrEqualRule, JClaveau\LogicalFilter\Rule\BetweenOrEqualBothRule, JClaveau\LogicalFilter\R...BetweenOrEqualLowerRule, JClaveau\LogicalFilter\R...BetweenOrEqualUpperRule, JClaveau\LogicalFilter\Rule\BetweenRule, JClaveau\LogicalFilter\Rule\InRule, JClaveau\LogicalFilter\Rule\NotEqualRule, JClaveau\LogicalFilter\Rule\NotInRule, JClaveau\LogicalFilter\Rule\NotRule, JClaveau\LogicalFilter\Rule\OrRule. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
59 48
        ) {
60 23
            $ruleTree = new OrRule([
61 23
                new AndRule([
62 23
                    $this,
63 23
                ]),
64 23
            ]);
65 23
        }
66 29
        elseif ($this instanceof AndRule) {
67 9
            $ruleTree = new OrRule([
68 9
                $this,
69 9
            ]);
70 9
        }
71 21
        elseif ($this instanceof OrRule) {
72 21
            foreach ($this->operands as $i => $operand) {
73 21
                if (! $operand instanceof AndRule) {
74 11
                    $this->operands[$i] = new AndRule([$operand]);
0 ignored issues
show
Bug introduced by
The property operands does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75 11
                }
76 21
            }
77 21
            $ruleTree = $this;
78 21
        }
79
        else {
80
            throw new \LogicException(
81
                "Unhandled type of simplified rules provided for conversion: "
82
                .$this
83
            );
84
        }
85
86 48
        if ($simplification_step_to_keep) {
87 44
            foreach ($operands = $ruleTree->getOperands() as $andOperand) {
88 44
                if (! $andOperand instanceof AndRule) {
89
                    throw new \LogicException(
90
                        "A rule is intended to be an and case: \n"
91
                        .$andOperand
92
                        ."\nof:\n"
93
                        .$ruleTree
94
                    );
95
                }
96
97 44
                $andOperand->moveSimplificationStepForward($simplification_step_to_keep, [], true);
98 44
            }
99 44
            $ruleTree->setOperands($operands);
100 44
            $ruleTree->moveSimplificationStepForward($simplification_step_to_keep, [], true);
101 44
        }
102
103
104 48
        return $ruleTree;
105
    }
106
107
    /**/
108
}
109