Completed
Push — work-fleets ( 9503d9...d28adc )
by SuperNova.WS
06:05
created

V2PropertyContainer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 102
Duplicated Lines 43.14 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 44
loc 102
rs 10
ccs 0
cts 53
cp 0
wmc 18
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperties() 0 3 1
A isEmpty() 0 3 1
A assignAccessor() 0 11 3
A __set() 7 7 2
A __get() 7 7 2
B importRow() 16 16 5
A exportRow() 14 14 4

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
use \Common\ContainerMagic;
4
use \Common\IPropertyContainer;
5
6
class V2PropertyContainer extends ContainerMagic implements IPropertyContainer {
7
8
  /**
9
   * Property descriptions
10
   *
11
   * @var array[]
12
   */
13
  protected $properties = array();
14
15
  /**
16
   * Array of accessors - getters/setters/etc
17
   *
18
   * Getter is a callable like
19
   *    function ($value) use ($that) {}
20
   *
21
   * Setter is a callable like
22
   *    function () use ($that) {}
23
   *
24
   * Importer is a callable like
25
   *    function (&$row) use ($this) {}
26
   *
27
   * Exporter is a callable like
28
   *    function (&$row) use ($this) {}
29
   *
30
   * @var callable[][]
31
   */
32
  protected $accessors;
33
34
  public function setProperties($properties) {
35
    $this->properties = $properties;
36
  }
37
38
  /**
39
   * Is container contains no data
40
   *
41
   * @return bool
42
   */
43
  public function isEmpty() {
44
    return empty($this->values);
45
  }
46
47
  public function assignAccessor($varName, $type, $callable) {
48
    if (empty($callable)) {
49
      return;
50
    }
51
52
    if (is_callable($callable)) {
53
      $this->accessors[$type][$varName] = $callable;
54
    } else {
55
      throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope');
56
    }
57
  }
58
59 View Code Duplication
  public function __set($name, $value) {
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...
60
    if (is_callable($this->accessors[P_CONTAINER_SETTER][$name])) {
61
      call_user_func($this->accessors[P_CONTAINER_SETTER][$name], $value);
62
    } else {
63
      $this->values[$name] = $value;
64
    }
65
  }
66
67 View Code Duplication
  public function __get($name) {
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...
68
    if (is_callable($this->accessors[P_CONTAINER_GETTER][$name])) {
69
      return call_user_func($this->accessors[P_CONTAINER_GETTER][$name]);
70
    } else {
71
      return $this->values[$name];
72
    }
73
  }
74
75 View Code Duplication
  public function importRow($row) {
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...
76
    if(empty($row)) {
77
      return;
78
    }
79
80
    // TODO - reset container ?
81
82
    foreach ($this->properties as $propertyName => $propertyData) {
83
      if (is_callable($this->accessors[P_CONTAINER_IMPORTER][$propertyName])) {
84
        call_user_func($this->accessors[P_CONTAINER_IMPORTER][$propertyName], &$row);
85
      } elseif (!empty($propertyData[P_DB_FIELD])) {
86
        $this->$propertyName = $row[$propertyData[P_DB_FIELD]];
87
      }
88
      // Otherwise it's internal field - filled and used internally
89
    }
90
  }
91
92 View Code Duplication
  public function exportRow() {
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...
93
    $row = array();
94
95
    foreach ($this->properties as $propertyName => $propertyData) {
96
      if (is_callable($this->accessors[P_CONTAINER_EXPORTER][$propertyName])) {
97
        call_user_func($this->accessors[P_CONTAINER_EXPORTER][$propertyName], &$row);
98
      } elseif (!empty($propertyData[P_DB_FIELD])) {
99
        $row[$propertyData[P_DB_FIELD]] = $this->$propertyName;
100
      }
101
      // Otherwise it's internal field - filled and used internally
102
    }
103
104
    return $row;
105
  }
106
107
}
108