Completed
Push — master ( 7a35bc...7eff79 )
by Krzysztof
05:49 queued 02:58
created

HeightCriteria   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 39
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHeight() 0 4 1
A setHeight() 0 4 1
A shouldBeApplied() 0 4 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 86.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use \KGzocha\Searcher\Criteria\CriteriaInterface;
4
use \KGzocha\Searcher\CriteriaBuilder\CriteriaBuilderInterface;
5
use \KGzocha\Searcher\Context\Doctrine\QueryBuilderSearchingContext;
6
use \KGzocha\Searcher\Context\SearchingContextInterface;
7
use \KGzocha\Searcher\CriteriaBuilder\Collection\CriteriaBuilderCollection;
8
use \KGzocha\Searcher\Criteria\Collection\CriteriaCollection;
9
use \KGzocha\Searcher\Searcher;
10
11
class HeightCriteria implements CriteriaInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /**
14
     * @var float height in meters
15
     */
16
    private $height;
17
18
    /**
19
     * @param null|float $height
20
     */
21
    public function __construct($height = null)
22
    {
23
        $this->height = $height;
24
    }
25
26
    /**
27
     * @return float
28
     */
29
    public function getHeight()
30
    {
31
        return $this->height;
32
    }
33
34
    /**
35
     * @param float $height
36
     */
37
    public function setHeight($height)
38
    {
39
        $this->height = (float) $height;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function shouldBeApplied()
46
    {
47
        return $this->height != null;
48
    }
49
}
50
51
class HeightCriteriaBuilder implements CriteriaBuilderInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
52
{
53
    /**
54
     * @param HeightCriteria               $criteria
55
     * @param QueryBuilderSearchingContext $searchingContext
56
     */
57
    public function buildCriteria(
58
        CriteriaInterface $criteria,
59
        SearchingContextInterface $searchingContext
60
    ) {
61
        $searchingContext
62
            ->getQueryBuilder()
63
            ->andWhere('t.height = :number')
64
            ->setParameter('number', $criteria->getHeight());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface KGzocha\Searcher\Criteria\CriteriaInterface as the method getHeight() does only exist in the following implementations of said interface: HeightCriteria.

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...
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function allowsCriteria(CriteriaInterface $criteria)
71
    {
72
        return $criteria instanceof HeightCriteria;
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function supportsSearchingContext(
79
        SearchingContextInterface $searchingContext
80
    ) {
81
        return $searchingContext instanceof QueryBuilderSearchingContext;
82
    }
83
}
84
85
/** @var \Doctrine\ORM\QueryBuilder $queryBuilder */
86
$queryBuilder = /** lets assume its already created QueryBuilder */null;
87
88
$criteria = new HeightCriteria(200);
89
$criteriaBuilder = new HeightCriteriaBuilder();
90
$context = new QueryBuilderSearchingContext($queryBuilder);
91
92
$searcher = new Searcher(
93
    new CriteriaBuilderCollection([$criteriaBuilder]),
94
    $context
95
);
96
97
$results = $searcher->search(new CriteriaCollection([$criteria]));
98
99
foreach ($results as $result) {
100
    var_dump($result);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($result); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
101
}
102