Completed
Pull Request — develop (#241)
by ANTHONIUS
08:03
created

AbstractPaginationQuery   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 7.95 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 0
dl 7
loc 88
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
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Solr\Filter;
10
11
12
use Solr\Bridge\Manager;
13
use Zend\Filter\Exception;
14
use Zend\Filter\FilterInterface;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
use Core\Entity\AbstractIdentifiableModificationDateAwareEntity as EntityType;
17
18
abstract class AbstractPaginationQuery implements FilterInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $sortPropertiesMap = array();
24
25
    /**
26
     * Store property name and converter to be used
27
     * during result conversion
28
     *
29
     * @var array
30
     */
31
    protected $propertiesMap = [];
32
33
    /**
34
     * @var Manager
35
     */
36
    protected $manager = null;
37
38
    public function __construct(Manager $manager)
39
    {
40
        $this->manager = $manager;
41
    }
42
43
    public function filter($value)
44
    {
45
        $query = new \SolrQuery();
46
        
47
        return $this->createQuery($value,$query);
48
    }
49
50
    /**
51
     * @param $sort
52
     * @return array
53
     */
54
    protected function filterSort($sort)
55
    {
56
        if ('-' == $sort{0}) {
57
            $sortProp = substr($sort, 1);
58
            $sortDir  = Manager::SORT_DESCENDING;
59
        } else {
60
            $sortProp = $sort;
61
            $sortDir = Manager::SORT_ASCENDING;
62
        }
63
64 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...
65
            $sortProp = $this->sortPropertiesMap[$sortProp];
66
67
            if (is_array($sortProp)) {
68
                return array_fill_keys($sortProp, $sortDir);
69
            }
70
        }
71
72
        return array($sortProp => $sortDir);
73
    }
74
75
    /**
76
     * Returs an array key => value for this pagination filter
77
     * to define custom solr result handler
78
     * @return array
79
     * @codeCoverageIgnore
80
     */
81
    public function getPropertiesMap()
82
    {
83
        return $this->propertiesMap;
84
    }
85
86
    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...
87
    {
88
        $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...
89
        return new static($manager);
90
    }
91
92
    /**
93
     * Returns class to be used for entity object creation
94
     *
95
     * @return string
96
     */
97
    abstract public function getEntityClass();
98
99
    /**
100
     * @param   array $params
101
     * @param   \SolrQuery $query
102
     * @return  \SolrQuery
103
     */
104
    abstract public function createQuery(array $params,$query);
105
}