Completed
Pull Request — master (#1052)
by
unknown
07:01
created

TransformedFinder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 11.57 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 9
dl 14
loc 121
ccs 0
cts 37
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 6 1
A findHybrid() 0 6 1
A findRawResult() 0 6 1
A moreLikeThis() 0 7 1
A search() 0 10 2
A findPaginated() 7 7 1
A findRawPaginated() 7 7 1
A createPaginatorAdapter() 0 6 1
A createRawPaginatorAdapter() 0 5 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
namespace FOS\ElasticaBundle\Finder;
4
5
use Elastica\Document;
6
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
7
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
8
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
9
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter;
10
use Pagerfanta\Pagerfanta;
11
use Elastica\SearchableInterface;
12
use Elastica\Query;
13
14
/**
15
 * Finds elastica documents and map them to persisted objects.
16
 */
17
class TransformedFinder implements PaginatedFinderInterface
18
{
19
    protected $searchable;
20
    protected $transformer;
21
22
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
23
    {
24
        $this->searchable  = $searchable;
25
        $this->transformer = $transformer;
26
    }
27
28
    /**
29
     * Search for a query string.
30
     *
31
     * @param string  $query
32
     * @param integer $limit
33
     * @param array   $options
34
     *
35
     * @return array of model objects
36
     **/
37
    public function find($query, $limit = null, $options = array())
38
    {
39
        $results = $this->search($query, $limit, $options);
40
41
        return $this->transformer->transform($results);
42
    }
43
44
    public function findHybrid($query, $limit = null, $options = array())
45
    {
46
        $results = $this->search($query, $limit, $options);
47
48
        return $this->transformer->hybridTransform($results);
49
    }
50
    
51
    public function findRawResult($query, $limit = null, $options = array())
52
    {
53
        $results = $this->search($query, $limit, $options);
54
55
        return $results;
56
    }
57
    /**
58
     * Find documents similar to one with passed id.
59
     *
60
     * @param integer $id
61
     * @param array   $params
62
     * @param array   $query
63
     *
64
     * @return array of model objects
65
     **/
66
    public function moreLikeThis($id, $params = array(), $query = array())
67
    {
68
        $doc = new Document($id);
69
        $results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Elastica\SearchableInterface as the method moreLikeThis() does only exist in the following implementations of said interface: Elastica\Type.

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...
70
71
        return $this->transformer->transform($results);
72
    }
73
74
    /**
75
     * @param $query
76
     * @param null|int $limit
77
     * @param array    $options
78
     *
79
     * @return array
80
     */
81
    protected function search($query, $limit = null, $options = array())
82
    {
83
        $queryObject = Query::create($query);
84
        if (null !== $limit) {
85
            $queryObject->setSize($limit);
86
        }
87
        $results = $this->searchable->search($queryObject, $options)->getResults();
88
89
        return $results;
90
    }
91
92
    /**
93
     * Gets a paginator wrapping the result of a search.
94
     *
95
     * @param string $query
96
     * @param array  $options
97
     *
98
     * @return Pagerfanta
99
     */
100 View Code Duplication
    public function findPaginated($query, $options = 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...
101
    {
102
        $queryObject = Query::create($query);
103
        $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
104
105
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
106
    }
107
    
108
    /**
109
     * {@inheritdoc}
110
     */
111 View Code Duplication
    public function findRawPaginated($query, $options = 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...
112
    {
113
        $queryObject = Query::create($query);
114
        $paginatorAdapter = $this->createRawPaginatorAdapter($queryObject, $options);
115
116
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function createPaginatorAdapter($query, $options = array())
123
    {
124
        $query = Query::create($query);
125
126
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function createRawPaginatorAdapter($query, $options = array())
133
    {
134
        $query = Query::create($query);
135
        return new RawPaginatorAdapter($this->searchable, $query, $options);
136
    }
137
}
138