SearchHandler::buildSpecification()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.6333
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 20
1
<?php
2
/**
3
 * HiAPI Yii2 base project for building API
4
 *
5
 * @link      https://github.com/hiqdev/hiapi
6
 * @package   hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiapi\commands;
12
13
use hiqdev\DataMapper\Query\Specification;
14
use hiqdev\DataMapper\Repository\RepositoryInterface;
15
use hiqdev\DataMapper\Repository\EntityManagerInterface;
16
17
/**
18
 * Class SearchHandler
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class SearchHandler
23
{
24
    /**
25
     * @var EntityManagerInterface
26
     */
27
    private $em;
28
29
    /**
30
     * SearchHandler constructor.
31
     *
32
     * @param EntityManagerInterface $em
33
     */
34
    public function __construct(EntityManagerInterface $em)
35
    {
36
        $this->em = $em;
37
    }
38
39
    /**
40
     * @return Specification
41
     */
42
    protected function createSpecification()
43
    {
44
        return new Specification();
45
    }
46
47
    /**
48
     * @param SearchCommand $command
49
     */
50
    public function handle(EntityCommandInterface $command)
51
    {
52
        $repo = $this->getRepository($command);
0 ignored issues
show
Compatibility introduced by
$command of type object<hiapi\commands\EntityCommandInterface> is not a sub-type of object<hiapi\commands\EntityCommand>. It seems like you assume a concrete implementation of the interface hiapi\commands\EntityCommandInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
53
        $specification = $this->buildSpecification($command);
54
55
        if ($command->count) {
0 ignored issues
show
Bug introduced by
Accessing count on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
            return $repo->count($specification);
57
        }
58
59
        return $repo->findAll($specification);
60
    }
61
62
    /**
63
     * @param EntityCommandInterface|SearchCommand $command
64
     * @return Specification
65
     */
66
    protected function buildSpecification(EntityCommandInterface $command)
67
    {
68
        $spec = $this->createSpecification();
69
        $spec->where(array_merge($command->filter, $command->where));
0 ignored issues
show
Bug introduced by
Accessing filter on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing where on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
70
71
        if (!$command->count) {
0 ignored issues
show
Bug introduced by
Accessing count on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
            if (strtolower($command->limit) === 'all') {
0 ignored issues
show
Bug introduced by
Accessing limit on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
73
                $command->limit = -1;
0 ignored issues
show
Bug introduced by
Accessing limit on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74
            }
75
            $limit = $command->limit ?? 25;
0 ignored issues
show
Bug introduced by
Accessing limit on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
76
            $spec->limit($limit);
77
            if ($limit !== -1) {
78
                $spec->offset((($command->page ?? 0) * $limit) - $limit);
0 ignored issues
show
Bug introduced by
Accessing page on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
79
            }
80
            $spec->with(array_merge($command->include, $command->with));
0 ignored issues
show
Bug introduced by
Accessing include on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing with on the interface hiapi\commands\EntityCommandInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
        }
82
83
        return $spec;
84
    }
85
86
    /**
87
     * @param SearchCommand $command
88
     * @return RepositoryInterface
89
     */
90
    protected function getRepository(EntityCommand $command)
91
    {
92
        return $this->em->getRepository($command->getEntityClass());
93
    }
94
}
95