Completed
Pull Request — master (#1118)
by Karel
05:28
created

AbstractElasticaToModelTransformer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 1
dl 3
loc 36
ccs 3
cts 7
cp 0.4286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setPropertyAccessor() 0 4 1
A getSortingClosure() 3 8 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
/**
4
 * This file is part of the FOSElasticaBundle project.
5
 *
6
 * (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Transformer;
13
14
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
15
16
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
17
{
18
    /**
19
     * PropertyAccessor instance.
20
     *
21
     * @var PropertyAccessorInterface
22
     */
23
    protected $propertyAccessor;
24
25
    /**
26
     * Set the PropertyAccessor instance.
27
     *
28
     * @param PropertyAccessorInterface $propertyAccessor
29
     */
30 9
    public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
31
    {
32 9
        $this->propertyAccessor = $propertyAccessor;
33 9
    }
34
35
    /**
36
     * Returns a sorting closure to be used with usort() to put retrieved objects
37
     * back in the order that they were returned by ElasticSearch.
38
     *
39
     * @param array $idPos
40
     * @param string $identifierPath
41
     * @return callable
42
     */
43
    protected function getSortingClosure(array $idPos, $identifierPath)
44
    {
45
        $propertyAccessor = $this->propertyAccessor;
46
47 View Code Duplication
        return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
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...
48
            return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
49
        };
50
    }
51
}
52