Completed
Pull Request — dev (#9)
by Arnaud
03:11
created

EntityLabelTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 66.67 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 20
loc 30
wmc 7
lcom 0
cbo 2
ccs 16
cts 17
cp 0.9412
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getEntityLabel() 20 20 7

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 LAG\AdminBundle\Admin\Behaviors;
4
5
use Symfony\Component\PropertyAccess\PropertyAccess;
6
7
trait EntityLabelTrait
8
{
9
    /**
10
     * Try to find a property to get a label from an entity. If found, it returns the property value through the
11
     * property accessor.
12
     *
13
     * @param $entity
14
     * @return string
15
     */
16 1 View Code Duplication
    public function getEntityLabel($entity)
1 ignored issue
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...
17
    {
18 1
        $label = '';
19 1
        $accessor = PropertyAccess::createPropertyAccessor();
20
21 1
        if ($accessor->isReadable($entity, 'label')) {
22 1
            $label = $accessor->getValue($entity, 'label');
23 1
        } else if ($accessor->isReadable($entity, 'title')) {
24 1
            $label = $accessor->getValue($entity, 'title');
25 1
        } else if ($accessor->isReadable($entity, 'name')) {
26
            $label = $accessor->getValue($entity, 'name');
27 1
        } else if ($accessor->isReadable($entity, '__toString')) {
28 1
            $label = $accessor->getValue($entity, '__toString');
29 1
        } else if ($accessor->isReadable($entity, 'content')) {
30 1
            $label = strip_tags(substr($label = $accessor->getValue($entity, 'content'), 0, 100));
31 1
        } else if ($accessor->isReadable($entity, 'id')) {
32 1
            $label = $accessor->getValue($entity, 'id');
33 1
        }
34 1
        return $label;
35
    }
36
}
37