Conditions | 11 |
Paths | 12 |
Total Lines | 45 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 5 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function __construct($data, $primary, array $options = []) |
||
63 | { |
||
64 | $this->data = []; |
||
65 | $this->raw = $data; |
||
66 | |||
67 | $primary = new PropertyPath($primary); |
||
68 | |||
69 | $snapshot = null; |
||
70 | $accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor(); |
||
71 | |||
72 | if (isset($options['snapshotClass'])) { |
||
73 | if (!class_exists($options['snapshotClass'])) { |
||
74 | throw new InvalidArgumentException(sprintf('The snapshot class "%s" does not seem to be loadable', $options['snapshotClass'])); |
||
75 | } |
||
76 | |||
77 | $refl = new ReflectionClass($options['snapshotClass']); |
||
78 | |||
79 | if (!$refl->isInstantiable() || !$refl->isSubclassOf('Totem\\AbstractSnapshot')) { |
||
80 | throw new InvalidArgumentException('A Snapshot Class should be instantiable and extends abstract class Totem\\AbstractSnapshot'); |
||
81 | } |
||
82 | |||
83 | $snapshot = $options['snapshotClass']; |
||
84 | } |
||
85 | |||
86 | if (!is_array($data) && !$data instanceof Traversable) { |
||
87 | throw new InvalidArgumentException(sprintf('An array or a Traversable was expected to take a snapshot of a collection, "%s" given', is_object($data) ? get_class($data) : gettype($data))); |
||
88 | } |
||
89 | |||
90 | foreach ($data as $key => $value) { |
||
91 | if (!is_int($key)) { |
||
92 | throw new InvalidArgumentException('The given array / Traversable is not a collection as it contains non numeric keys'); |
||
93 | } |
||
94 | |||
95 | if (!$accessor->isReadable($value, $primary)) { |
||
96 | throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $primary)); |
||
97 | } |
||
98 | |||
99 | $primaryKey = $accessor->getValue($value, $primary); |
||
100 | |||
101 | $this->link[$primaryKey] = $key; |
||
102 | $this->data[$primaryKey] = $this->snapshot($value, $snapshot); |
||
103 | } |
||
104 | |||
105 | parent::normalize(); |
||
106 | } |
||
107 | |||
145 |