Completed
Push — dev ( e5a73e...d4e169 )
by Arnaud
03:54 queued 03:48
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 93.75%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 7
c 2
b 2
f 0
lcom 0
cbo 2
dl 20
loc 30
ccs 15
cts 16
cp 0.9375
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)
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
        }
34 1
        return $label;
35
    }
36
}
37