Completed
Push — work-fleets ( 1f2a61...18e75e )
by SuperNova.WS
07:59
created

V2PropertyContainer   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 24
loc 141
ccs 0
cts 56
cp 0
rs 10
wmc 19
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __isset() 0 3 1
A __unset() 0 3 1
A __set() 0 7 2
A __get() 0 7 2
A importRow() 10 10 4
A exportRow() 14 14 4
A setProperties() 0 3 1
A assignAccessor() 0 11 3
A isEmpty() 0 3 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
use \Common\IMagicProperties;
4
use \Common\IPropertyContainer;
5
6
class V2PropertyContainer implements IMagicProperties, IPropertyContainer {
7
8
  /**
9
   * Property descriptions
10
   *
11
   * @var array[]
12
   */
13
  protected $properties = array();
14
15
  /**
16
   * Property values
17
   *
18
   * @var array
19
   */
20
  protected $values = array();
21
  /**
22
   * Array of getters
23
   *
24
   * Getter is a callable like
25
   *    function ($value) use ($that) {}
26
   *
27
   * @var callable[]
28
   */
29
  protected $setters = array();
30
  /**
31
   * Array of setters
32
   *
33
   * Setter is a callable like
34
   *    function () use ($that) {}
35
   *
36
   * @var callable[]
37
   */
38
  protected $getters = array();
39
  /**
40
   * Array of importers
41
   *
42
   * Importer is a callable like
43
   *    function (&$row) use ($this) {}
44
   *
45
   * @var callable[]
46
   */
47
  protected $importers;
48
  /**
49
   * Array of exporters
50
   *
51
   * Exporter is a callable like
52
   *    function (&$row) use ($this) {}
53
   *
54
   * @var callable[]
55
   */
56
  protected $exporters = array();
57
58
  /**
59
   * Magic checker for property set
60
   *
61
   * @param string $name
62
   *
63
   * @return boolean
64
   */
65
  public function __isset($name) {
66
    return isset($this->values[$name]);
67
  }
68
69
  /**
70
   * Magic un-setter
71
   *
72
   * @param string $name
73
   */
74
  public function __unset($name) {
75
    unset($this->values[$name]);
76
  }
77
78
  public function __set($name, $value) {
79
    if (is_callable($this->setters[$name])) {
80
      call_user_func($this->setters[$name], $value);
81
    } else {
82
      $this->values[$name] = $value;
83
    }
84
  }
85
86
  public function __get($name) {
87
    if (is_callable($this->getters[$name])) {
88
      return call_user_func($this->getters[$name]);
89
    } else {
90
      return $this->values[$name];
91
    }
92
  }
93
94 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...
95
    foreach ($this->properties as $propertyName => $propertyData) {
96
      if (is_callable($this->importers[$propertyName])) {
97
        call_user_func($this->importers[$propertyName], &$row);
98
      } elseif (!empty($propertyData[P_DB_FIELD])) {
99
        $this->$propertyName = $row[$propertyData[P_DB_FIELD]];
100
      }
101
      // Otherwise it's internal field - filled and used internally
102
    }
103
  }
104
105 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...
106
    $row = array();
107
108
    foreach ($this->properties as $propertyName => $propertyData) {
109
      if (is_callable($this->exporters[$propertyName])) {
110
        call_user_func($this->exporters[$propertyName], &$row);
111
      } elseif (!empty($propertyData[P_DB_FIELD])) {
112
        $row[$propertyData[P_DB_FIELD]] = $this->$propertyName;
113
      }
114
      // Otherwise it's internal field - filled and used internally
115
    }
116
117
    return $row;
118
  }
119
120
  public function setProperties($properties) {
121
    $this->properties = $properties;
122
  }
123
124
  public function assignAccessor($varName, $type, $callable) {
125
    if (empty($callable)) {
126
      return;
127
    }
128
129
    if (is_callable($callable)) {
130
      $this->{$type}[$varName] = $callable;
131
    } else {
132
      throw new Exception('Error assigning callable in ' . get_called_class() . '! Callable typed [' . $type . '] is not a callable or not accessible in the scope');
133
    }
134
  }
135
136
137
  /**
138
   * Is container contains no data
139
   *
140
   * @return bool
141
   */
142
  public function isEmpty() {
143
    return empty($this->values);
144
  }
145
146
}
147