Completed
Pull Request — develop (#241)
by ANTHONIUS
07:36
created

AbstractPaginationQuery   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 6.54 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 6 1
A filterSort() 7 20 4
A getPropertiesMap() 0 4 1
A factory() 0 5 1
getEntityClass() 0 1 ?
createQuery() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Solr\Filter;
11
12
13
use Solr\Bridge\Manager;
14
use Zend\Filter\Exception;
15
use Zend\Filter\FilterInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
18
/**
19
 * Class AbstractPaginationQuery
20
 *
21
 * @author  Anthonius Munthi <[email protected]>
22
 * @since   0.27
23
 * @package Solr\Filter
24
 */
25
abstract class AbstractPaginationQuery implements FilterInterface
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $sortPropertiesMap = array();
31
32
    /**
33
     * Store property name and converter to be used
34
     * during result conversion
35
     *
36
     * @var array
37
     */
38
    protected $propertiesMap = [];
39
40
    /**
41
     * @var Manager
42
     */
43
    protected $manager = null;
44
45
    /**
46
     * AbstractPaginationQuery constructor.
47
     * 
48
     * @param Manager $manager
49
     */
50
    public function __construct(Manager $manager)
51
    {
52
        $this->manager = $manager;
53
    }
54
55
    /**
56
     * Filter query based on given value
57
     *
58
     * @param mixed $value
59
     * @return \SolrQuery
60
     */
61
    public function filter($value)
62
    {
63
        $query = new \SolrQuery();
64
        
65
        return $this->createQuery($value,$query);
66
    }
67
68
    /**
69
     * Returns sort parameter to be used for query
70
     *
71
     * @param $sort
72
     * @return array
73
     */
74
    protected function filterSort($sort)
75
    {
76
        if ('-' == $sort{0}) {
77
            $sortProp = substr($sort, 1);
78
            $sortDir  = Manager::SORT_DESCENDING;
79
        } else {
80
            $sortProp = $sort;
81
            $sortDir = Manager::SORT_ASCENDING;
82
        }
83
84 View Code Duplication
        if (isset($this->sortPropertiesMap[$sortProp])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
            $sortProp = $this->sortPropertiesMap[$sortProp];
86
87
            if (is_array($sortProp)) {
88
                return array_fill_keys($sortProp, $sortDir);
89
            }
90
        }
91
92
        return array($sortProp => $sortDir);
93
    }
94
95
    /**
96
     * Returs an array key => value for this pagination filter
97
     * to define custom solr result handler
98
     * @return array
99
     * @codeCoverageIgnore
100
     */
101
    public function getPropertiesMap()
102
    {
103
        return $this->propertiesMap;
104
    }
105
106
    /**
107
     * Creates new instance for this filter
108
     *
109
     * @param ServiceLocatorInterface $sl
110
     * @return static
111
     */
112
    static public function factory(ServiceLocatorInterface $sl)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
113
    {
114
        $manager = $sl->getServiceLocator()->get('Solr/Manager');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\ServiceManager\ServiceLocatorInterface as the method getServiceLocator() does only exist in the following implementations of said interface: Acl\Assertion\AssertionManager, Core\Mail\MailService, Core\Paginator\PaginatorService, Zend\Barcode\ObjectPluginManager, Zend\Barcode\RendererPluginManager, Zend\Cache\PatternPluginManager, Zend\Cache\Storage\AdapterPluginManager, Zend\Cache\Storage\PluginManager, Zend\Config\ReaderPluginManager, Zend\Config\WriterPluginManager, Zend\Feed\Reader\ExtensionPluginManager, Zend\Feed\Writer\ExtensionPluginManager, Zend\File\Transfer\Adapter\FilterPluginManager, Zend\File\Transfer\Adapter\ValidatorPluginManager, Zend\Filter\FilterPluginManager, Zend\Form\FormElementMan...lementManagerV2Polyfill, Zend\Form\FormElementMan...lementManagerV3Polyfill, Zend\Hydrator\HydratorPluginManager, Zend\I18n\Translator\LoaderPluginManager, Zend\InputFilter\InputFilterPluginManager, Zend\Log\FilterPluginManager, Zend\Log\FormatterPluginManager, Zend\Log\ProcessorPluginManager, Zend\Log\WriterPluginManager, Zend\Log\Writer\FilterPluginManager, Zend\Log\Writer\FormatterPluginManager, Zend\Mail\Protocol\SmtpPluginManager, Zend\Mvc\Controller\ControllerManager, Zend\Mvc\Controller\PluginManager, Zend\Mvc\Router\RoutePluginManager, Zend\Paginator\AdapterPluginManager, Zend\Paginator\ScrollingStylePluginManager, Zend\Serializer\AdapterPluginManager, Zend\ServiceManager\AbstractPluginManager, Zend\Tag\Cloud\DecoratorPluginManager, Zend\Text\Table\DecoratorManager, Zend\Validator\ValidatorPluginManager, Zend\View\HelperPluginManager, Zend\View\Helper\Navigation\PluginManager.

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...
115
        return new static($manager);
116
    }
117
118
    /**
119
     * Returns class to be used for entity object creation
120
     *
121
     * @return string
122
     */
123
    abstract public function getEntityClass();
124
125
    /**
126
     * @param   array $params
127
     * @param   \SolrQuery $query
128
     * @return  \SolrQuery
129
     */
130
    abstract public function createQuery(array $params,$query);
131
}