Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

BusinessPageRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 8
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B findPageByBusinessEntityAndPattern() 8 26 3

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 Victoire\Bundle\BusinessPageBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Symfony\Component\PropertyAccess\PropertyAccessor;
7
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity;
8
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
9
10
/**
11
 * The Business Entity Page repository.
12
 */
13
class BusinessPageRepository extends EntityRepository
14
{
15
    /**
16
     *  Find the pagePatterns of the business entity.
17
     *
18
     * @param BusinessTemplate $pattern
19
     * @param object           $entity         | int $entityId
20
     * @param BusinessEntity   $businessEntity
21
     *
22
     * @return mixed
23
     */
24
    public function findPageByBusinessEntityAndPattern(BusinessTemplate $pattern, $entity, BusinessEntity $businessEntity)
25
    {
26 View Code Duplication
        if (is_object($entity)) {
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...
27
            $accessor = new PropertyAccessor();
28
            if (method_exists($entity, 'getId')) {
29
                $entityId = $entity->getId();
30
            } else {
31
                $entityId = $accessor->getValue($entity, $businessEntity->getBusinessIdentifiers()->first()->getName());
32
            }
33
        }
34
        $qb = $this->createQueryBuilder('BusinessPage');
35
        $qb->join('BusinessPage.entityProxy', 'proxy');
36
        $qb->join('BusinessPage.template', 'template');
37
        $qb->join('proxy.businessEntity', 'businessEntity');
38
39
        $qb->where('template.id = :templateId');
40
41
        $qb->andWhere('businessEntity.name = :entityName');
42
        $qb->andWhere('proxy.ressourceId = :entityId');
43
44
        $qb->setParameter(':templateId', $pattern);
45
        $qb->setParameter(':entityId', $entityId);
0 ignored issues
show
Bug introduced by
The variable $entityId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
46
        $qb->setParameter(':entityName', $businessEntity->getName());
47
48
        return $qb->getQuery()->getOneOrNullResult();
49
    }
50
}
51