Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

CountAttributeTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 40
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountAttribute() 12 12 3
load() 1 1 ?
getRelation() 1 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 Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
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 Tinyissue\Extensions\Model;
13
14
/**
15
 * CountAttributeTrait is trait class for adding method to return count attribute.
16
 *
17
 * @author Mohamed Alsharaf <[email protected]>
18
 *
19
 * @property static $this
20
 */
21 View Code Duplication
trait CountAttributeTrait
0 ignored issues
show
Duplication introduced by
This class 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...
22
{
23
    /**
24
     * Returns the aggregate value of a field.
25
     *
26
     * @param string $field
27
     *
28
     * @return int
29
     */
30
    protected function getCountAttribute($field)
31
    {
32
        // if relation is not loaded already, let's do it first
33
        if (!array_key_exists($field, $this->relations)) {
0 ignored issues
show
Bug introduced by
The property relations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
            $this->load($field);
35
        }
36
37
        $related = $this->getRelation($field);
38
39
        // then return the count directly
40
        return ($related) ? (int) $related->aggregate : 0;
41
    }
42
43
    /**
44
     * Eager load relations on the model.
45
     *
46
     * @param array|string $relations
47
     *
48
     * @return $this
49
     */
50
    abstract public function load($relations);
51
52
    /**
53
     * Get a specified relationship.
54
     *
55
     * @param string $relation
56
     *
57
     * @return mixed
58
     */
59
    abstract public function getRelation($relation);
60
}
61