GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BAssociation   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 4.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 4
loc 98
rs 10
c 0
b 0
f 0
ccs 30
cts 45
cp 0.6667
wmc 16
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A updateAssociations() 0 23 5
A deleteAssociations() 0 17 3
A getSrc() 0 4 1
A getChecked() 0 9 3
A getAssociatedKeys() 4 14 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
 * @author Nikita Melnikov <[email protected]>, Sergey Glagolev <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package backend.models
8
 *
9
 * @method static BAssociation model(string $class = __CLASS__)
10
 *
11
 * @property string $src
12
 * @property string $src_frontend
13
 * @property integer $src_id
14
 * @property string $dst
15
 * @property string $dst_frontend
16
 * @property integer $dst_id
17
 */
18
class BAssociation extends BActiveRecord
19
{
20
  /**
21
   * @return string
22
   */
23 3
  public function tableName()
24
  {
25 3
    return '{{association}}';
26
  }
27
28
  /**
29
   * @param BActiveRecord $model
30
   * @param string        $dst
31
   * @param array         $ids
32
   * @param bool          $deleteAssociations
33
   */
34 3
  public function updateAssociations(BActiveRecord $model, $dst, array $ids, $deleteAssociations = true)
35
  {
36
    if( $deleteAssociations )
37 3
      self::model()->deleteAssociations($model, $dst);
38
39 3
    foreach($ids as $id)
40
    {
41 3
      $association         = new BAssociation();
42 3
      $association->src    = $this->getSrc($model);
43 3
      $association->src_id = $model->getPrimaryKey();
44 3
      $association->dst    = $dst;
45 3
      $association->dst_id = $id;
46
47 3
      if( $model instanceof IHasFrontendModel )
48 3
        $association->src_frontend = $model->getFrontendModelName();
49
50 3
      $dstModel = new $dst();
51 3
      if( $dstModel instanceof IHasFrontendModel )
52 3
        $association->dst_frontend = $dstModel->getFrontendModelName();
53
54 3
      $association->save();
55 3
    }
56 3
  }
57
58
  /**
59
   * @param BActiveRecord $model
60
   * @param null|string   $dst
61
   * @param null|string   $dstId
62
   */
63 3
  public function deleteAssociations(BActiveRecord $model, $dst = null, $dstId = null)
64
  {
65 3
    $src   = $this->getSrc($model);
66 3
    $srcId = $model->getPrimaryKey();
67
68 3
    $criteria = new CDbCriteria();
69 3
    $criteria->compare('src', $src);
70 3
    $criteria->compare('src_id', $srcId);
71
72
    if( $dst )
0 ignored issues
show
Bug Best Practice introduced by
The expression $dst of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73 3
      $criteria->compare('dst', $dst);
74
75
    if( $dstId )
0 ignored issues
show
Bug Best Practice introduced by
The expression $dstId of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
76 3
      $criteria->compare('dst_id', $dstId);
77
78 3
    $this->deleteAll($criteria);
79 3
  }
80
81
  /**
82
   * @param BActiveRecord $model
83
   *
84
   * @return string
85
   */
86 5
  public function getSrc(BActiveRecord $model)
87
  {
88 5
    return get_class($model);
89
  }
90
91
  public function getChecked($parameters)
92
  {
93
    $criteria = new CDbCriteria();
94
95
    foreach($parameters as $key => $value)
96
      $criteria->compare($key, $value);
97
98
    return $this->count($criteria) ? true : false;
99
  }
100
101
  public function getAssociatedKeys($data = null)
102
  {
103 View Code Duplication
    if( !isset($data) )
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...
104
    {
105
      $data = Arr::extract($_GET, array('src', 'dst', 'srcId'));
106
    }
107
108
    if( isset($data['srcId']) )
109
    {
110
      $data['src_id'] = Arr::cut($data, 'srcId');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data['src_id'] is correct as \Arr::cut($data, 'srcId') (which targets Arr::cut()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
111
    }
112
113
    return CHtml::listData(self::model()->findAllByAttributes($data), 'dst_id', 'dst_id');
114
  }
115
}