Completed
Pull Request — master (#1571)
by
unknown
03:13
created

PHPCRPagerProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 59
Duplicated Lines 40.68 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 24
loc 59
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A provide() 17 17 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 package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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\Doctrine;
13
14
use Doctrine\Persistence\ManagerRegistry;
15
use FOS\ElasticaBundle\Provider\PagerfantaPager;
16
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
17
use Pagerfanta\Adapter\DoctrineODMPhpcrAdapter;
18
use Pagerfanta\Pagerfanta;
19
20
final class PHPCRPagerProvider implements PagerProviderInterface
21
{
22
    const ENTITY_ALIAS = 'a';
23
24
    /**
25
     * @var string
26
     */
27
    private $objectClass;
28
29
    /**
30
     * @var ManagerRegistry
31
     */
32
    private $doctrine;
33
34
    /**
35
     * @var array
36
     */
37
    private $baseOptions;
38
    
39
    /**
40
     * @var RegisterListenersService
41
     */
42
    private $registerListenersService;
43
44
    /**
45
     * @param ManagerRegistry $doctrine
46
     * @param RegisterListenersService $registerListenersService
47
     * @param string $objectClass
48
     * @param array $baseOptions
49
     */
50 View Code Duplication
    public function __construct(ManagerRegistry $doctrine, RegisterListenersService $registerListenersService, $objectClass, array $baseOptions)
51
    {
52
        $this->doctrine = $doctrine;
53
        $this->objectClass = $objectClass;
54
        $this->baseOptions = $baseOptions;
55
        $this->registerListenersService = $registerListenersService;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 View Code Duplication
    public function provide(array $options = array())
62
    {
63
        $options = array_replace($this->baseOptions, $options);
64
65
        $manager = $this->doctrine->getManagerForClass($this->objectClass);
66
        $repository = $manager->getRepository($this->objectClass);
67
        
68
        $adapter = new DoctrineODMPhpcrAdapter(
0 ignored issues
show
Deprecated Code introduced by
The class Pagerfanta\Adapter\DoctrineODMPhpcrAdapter has been deprecated with message: to be removed in 3.0, use the `Pagerfanta\Doctrine\PHPCRODM\QueryAdapter` from the `pagerfanta/doctrine-phpcr-odm-adapter` package instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
69
            call_user_func([$repository, $options['query_builder_method']], static::ENTITY_ALIAS)
70
        );
71
        
72
        $pager = new PagerfantaPager(new Pagerfanta($adapter));
73
        
74
        $this->registerListenersService->register($manager, $pager, $options);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->doctrine->getMana...ass($this->objectClass) on line 65 can be null; however, FOS\ElasticaBundle\Doctr...nersService::register() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
75
76
        return $pager;
77
    }
78
}
79