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 declare(strict_types=1); |
||
24 | class ObjectMap implements ObjectMapInterface |
||
25 | { |
||
26 | use ObjectTypeHintTrait; |
||
27 | |||
28 | protected $behavior = self::DEFAULT; |
||
29 | |||
30 | /** |
||
31 | * @var Bucket[] |
||
32 | */ |
||
33 | protected $keys = []; |
||
34 | |||
35 | /** |
||
36 | * @param int $behavior |
||
37 | */ |
||
38 | 42 | public function __construct(int $behavior = self::DEFAULT) |
|
39 | { |
||
40 | 42 | $this->behavior = $behavior; |
|
41 | 42 | } |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 28 | public function put($key, $value) |
|
47 | { |
||
48 | 28 | $this->assertObject($key, 'Key'); |
|
49 | 27 | $this->assertObject($value, 'Value'); // while we may associate non-object value, for interface compatibility we don't do that |
|
50 | |||
51 | 26 | $hash = $this->getHash($key); |
|
52 | |||
53 | 26 | if (isset($this->keys[$hash])) { |
|
54 | 1 | throw new OverflowException('Value with such key already exists'); |
|
55 | } |
||
56 | |||
57 | 26 | $bucket = $this->createBucket($key, $value, $hash); |
|
58 | |||
59 | 26 | $this->keys[$hash] = $bucket; |
|
60 | 26 | } |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 9 | View Code Duplication | public function get($key) |
66 | { |
||
67 | 9 | $this->assertObject($key, 'Key'); |
|
68 | |||
69 | 8 | $hash = $this->getHash($key); |
|
70 | |||
71 | 8 | if (!isset($this->keys[$hash])) { |
|
72 | 2 | throw new OutOfBoundsException('Value with such key not found'); |
|
73 | } |
||
74 | |||
75 | 6 | $bucket = $this->keys[$hash]; |
|
76 | |||
77 | 6 | return $this->fetchBucketValue($bucket); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 23 | public function has($key): bool |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 7 | View Code Duplication | public function remove($key) |
96 | { |
||
97 | 7 | $this->assertObject($key, 'Key'); |
|
98 | |||
99 | 6 | $hash = $this->getHash($key); |
|
100 | |||
101 | 6 | if (!isset($this->keys[$hash])) { |
|
102 | 1 | throw new OutOfBoundsException('Value with such key not found'); |
|
103 | } |
||
104 | |||
105 | 5 | $bucket = $this->keys[$hash]; |
|
106 | |||
107 | 5 | $this->doRemove($hash); |
|
108 | |||
109 | 5 | return $this->fetchBucketValue($bucket); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 16 | public function count() |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 3 | public function clear() |
|
127 | |||
128 | /** |
||
129 | * @param object $value |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 33 | protected function getHash($value) |
|
137 | |||
138 | /** |
||
139 | * @param string $hash |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | 14 | protected function doRemove(string $hash) |
|
147 | |||
148 | /** |
||
149 | * @param object $key |
||
150 | * @param object $value |
||
151 | * @param string $hash |
||
152 | * |
||
153 | * @return Bucket |
||
154 | */ |
||
155 | 26 | protected function createBucket($key, $value, string $hash): Bucket |
|
167 | |||
168 | /** |
||
169 | * @param Bucket $bucket |
||
170 | * |
||
171 | * @return null|object |
||
172 | */ |
||
173 | 11 | protected function fetchBucketValue(Bucket $bucket) |
|
183 | |||
184 | /** |
||
185 | * @param $obj |
||
186 | * @param string $hash |
||
187 | * |
||
188 | * @return WeakReference |
||
189 | */ |
||
190 | protected function createReference($obj, string $hash): WeakReference |
||
196 | } |
||
197 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.