MinimalConverter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B convert() 0 45 9
1
<?php
2
/**
3
 * MinimalConverter
4
 *
5
 * @package php-logical-filter
6
 * @author  Jean Claveau
7
 */
8
namespace JClaveau\LogicalFilter\Converter;
9
10
use       JClaveau\LogicalFilter\Converter\ConverterInterface;
11
use       JClaveau\LogicalFilter\LogicalFilter;
12
13
/**
14
 * Basic tools to implements minimal converters: Converters that can
15
 * handle simplified filters with or/and/atomic structure.
16
 */
17
abstract class MinimalConverter implements ConverterInterface
18
{
19
    /**
20
     * @param LogicalFilter $filter
21
     */
22 5
    public function convert( LogicalFilter $filter )
23
    {
24 5
        $rootOr = $filter->simplify(['force_logical_core' => true])->getRules();
25
26
        // TODO remove this once TrueRule implemented https://github.com/jclaveau/php-logical-filter/issues/59
27 5
        if (null === $rootOr) {
28 2
            return $this;
29
        }
30
31 3
        if ( ! $rootOr->hasSolution()) {
32
            return $this;
33
        }
34
35 3
        foreach ($rootOr->getOperands() as $andOperand) {
36 3
            $this->onOpenOr();
37 3
            $operandsByFields = $andOperand->groupOperandsByFieldAndOperator();
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 groupOperandsByFieldAndOperator() 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...
38
39 3
            foreach ($operandsByFields as $field => $operandsByOperator) {
40 3
                foreach ($operandsByOperator as $operator => $operandsOfOperator) {
41 3
                    if (1 != count($operandsOfOperator)) {
42
                        throw new \RuntimeException(
43
                             "Once a logical filter is simplified, there MUST be "
44
                            ."no more than one operand by operator instead of for '$field' / '$operator': "
45
                            .var_export($operandsOfOperator, true)
46
                        );
47
                    }
48
49 3
                    $operandsByFields[ $field ][ $operator ] = $operandsOfOperator[0];
50 3
                }
51 3
            }
52
53 3
            foreach ($operandsByFields as $field => $operandsByOperator) {
54 3
                foreach ($operandsByOperator as $operator => $operand) {
55 3
                    $this->onAndPossibility(
56 3
                        $field,
57 3
                        $operator,
58 3
                        $operand,
59
                        $operandsByFields
60 3
                    );
61 3
                }
62 3
            }
63
64 3
            $this->onCloseOr();
65 3
        }
66 3
    }
67
68
    /**/
69
}
70