Completed
Push — master ( d5a6c2...06b50b )
by Vitaly
02:40
created

Entity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 20.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 2
dl 20
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A idsByNavigationID() 0 13 2
A byIDs() 0 7 1
A byNavigationID() 10 10 2
A amountByNavigationID() 10 10 2

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
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 08.12.15
6
 * Time: 22:14
7
 */
8
namespace samsoncms\api;
9
10
use samsonframework\orm\QueryInterface;
11
12
/**
13
 * Generic SamsonCMS entity which has Navigation relation.
14
 * @package samsoncms\api
15
 */
16
class Entity
17
{
18
    /** Deletion flag field name */
19
    const DELETE_FLAG_FIELD = 'Active';
20
21
    /** @var QueryInterface Database query instance */
22
    protected $query;
23
24
    /** @var string Entity identifier */
25
    protected $identifier;
26
27
    /** @var string Entity primary field name */
28
    protected $primaryField;
29
30
    /**
31
     * Entity constructor.
32
     * @param QueryInterface $query Database query instance
33
     * @param string $identifier Entity identifier
34
     */
35
    public function __construct(QueryInterface $query, $identifier)
36
    {
37
        $this->query = $query;
38
        $this->identifier = $identifier;
39
        $this->primaryField = $identifier::$_primary;
40
    }
41
42
    /**
43
     * Get current entity identifiers collection by navigation identifier.
44
     *
45
     * @param string $navigationID Navigation identifier
46
     * @param array $filteringIDs Collection of entity identifiers for filtering query
47
     * @return array Collection of entity identifiers filtered by navigation identifier.
48
     */
49
    public function idsByNavigationID($navigationID, $filteringIDs = null)
50
    {
51
        // Prepare query
52
        $this->query->where(Navigation::$_primary, $navigationID)->where(self::DELETE_FLAG_FIELD, 1);
53
54
        // Add material identifier filter if passed
55
        if (isset($filteringIDs)) {
56
            $this->query->where($this->primaryField, $filteringIDs);
0 ignored issues
show
Documentation introduced by
$filteringIDs is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
        }
58
59
        // Perform database query and get only material identifiers collection
60
        return $this->query->fields($this->primaryField);
61
    }
62
63
    /**
64
     * Get current entity instances collection by their identifiers.
65
     * Method can accept different query executors.
66
     *
67
     * @param string|array $entityIDs Entity identifier or their collection
68
     * @param string $executor Method name for query execution
69
     * @return self[] Collection of entity instances
70
     */
71
    public function byIDs($entityIDs, $executor = 'exec')
72
    {
73
        return $this->query
74
            ->where($this->primaryField, $entityIDs)
0 ignored issues
show
Bug introduced by
It seems like $entityIDs defined by parameter $entityIDs on line 71 can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
75
            ->where(self::DELETE_FLAG_FIELD, 1)
76
            ->$executor();
77
    }
78
79
    /**
80
     * Get current entity instances collection by navigation identifier.
81
     *
82
     * @param string $navigationID Navigation identifier
83
     * @return self[] Collection of entity instances
84
     */
85 View Code Duplication
    public function byNavigationID($navigationID)
0 ignored issues
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...
86
    {
87
        $return = array();
88
        /** @var array $materialIds Collection of entity identifiers filtered by additional field */
89
        if (sizeof($materialIds = $this->idsByNavigationID($navigationID))) {
90
            $return = $this->byIDs($materialIds);
0 ignored issues
show
Documentation introduced by
$materialIds is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        }
92
93
        return $return;
94
    }
95
96
    /**
97
     * Get current entity instances amount by navigation identifier.
98
     *
99
     * @param string $navigationID Navigation identifier
100
     * @return integer Amount of entities related to Navigation identifier
101
     */
102 View Code Duplication
    public function amountByNavigationID($navigationID)
0 ignored issues
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...
103
    {
104
        $return =0;
105
        /** @var array $materialIds Collection of entity identifiers filtered by additional field */
106
        if (sizeof($materialIds = $this->idsByNavigationID($navigationID))) {
107
            $return = $this->byIDs($materialIds, 'count');
0 ignored issues
show
Documentation introduced by
$materialIds is of type boolean|object<samsonfra...rk\orm\RecordInterface>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
        }
109
110
        return $return;
111
    }
112
}
113