Completed
Pull Request — master (#7)
by Dmitry
28:59 queued 14:05
created

SubfiltersProducer::produceAsGeneric()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace hiapi\commands\Search\Filter;
4
5
use hiapi\commands\Search\Filter\Type\ArrayOfFilter;
6
use hiapi\commands\Search\Filter\Type\FilterInterface;
7
use hiapi\commands\Search\Filter\Type\GenericFilter;
8
use hiapi\commands\Search\Filter\Type\IntegerFilter;
9
10
class SubfiltersProducer
11
{
12
    /**
13
     * @psalm-var array<class-string<FilterInterface>, array<string, class-string<FilterInterface>|list<class-string<FilterInterface>>>>
14
     */
15
    private $map = [
16
        // Either this way
17
        IntegerFilter::class => [
18
            '_ne' => IntegerFilter::class,
19
            '_gt' => IntegerFilter::class,
20
            '_lt' => IntegerFilter::class,
21
            '_in' => [ArrayOfFilter::class, IntegerFilter::class],
22
            '_ni' => [ArrayOfFilter::class, IntegerFilter::class],
23
        ]
24
    ];
25
26
    /**
27
     * @psalm-var array<
28
        string,
29
        array<string, string|array{class-string<FilterInterface>, string}>
30
      >
31
     */
32
    private $constantsMap = [
33
        // OR this way.
34
        // Cons:
35
        //  - no real namespaces for constant values
36
        //  - filter name should be passed nearby as a separate property
37
        GenericFilter::INTEGER => [
38
            '_ne' => GenericFilter::INTEGER,
39
            '_gt' => GenericFilter::INTEGER,
40
            '_lt' => GenericFilter::INTEGER,
41
            '_in' => [ArrayOfFilter::class, GenericFilter::INTEGER],
42
            '_ni' => [ArrayOfFilter::class, GenericFilter::INTEGER],
43
        ]
44
    ];
45
46
    /**
47
     * @param FilterInterface $filter
48
     * @return FilterInterface[]
49
     * @psalm-return list<FilterInterface>
50
     */
51
    public function produce(FilterInterface $filter): array
52
    {
53
        return $filter instanceof GenericFilter
54
            ? $this->produceAsGeneric($filter)
55
            : $this->produceAsSeparateClass($filter);
56
    }
57
58 View Code Duplication
    private function produceAsGeneric(FilterInterface $filter): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $type = $filter->type();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface hiapi\commands\Search\Filter\Type\FilterInterface as the method type() does only exist in the following implementations of said interface: hiapi\commands\Search\Filter\Type\GenericFilter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
        if (!isset($this->constantsMap[$type])) {
62
            return [];
63
        }
64
65
        $result = [];
66
        foreach ($this->constantsMap[$type] as $suffix => $newType) {
67
            $attributeName = $filter->name() . $suffix;
68
69
            if (is_array($newType)) {
70
                $className = array_shift($newType);
71
                $result[] = new $className($attributeName, $newType[0]);
72
            } else {
73
                $result[] = GenericFilter::__callStatic($newType, [$attributeName]);
74
            }
75
        }
76
77
        return $result;
78
    }
79
80 View Code Duplication
    private function produceAsSeparateClass(FilterInterface $filter): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $type = get_class($filter);
83
        if (!isset($this->map[$type])) {
84
            return [];
85
        }
86
87
        $result = [];
88
        foreach ($this->map[$type] as $suffix => $newType) {
89
            $attributeName = $filter->name() . $suffix;
90
91
            if (is_array($newType)) {
92
                $className = array_shift($newType);
93
                $result[] = new $className($attributeName, $newType[0]);
94
            } else {
95
                $result[] = new $newType($attributeName);
96
            }
97
        }
98
99
        return $result;
100
    }
101
}
102