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 |
||
29 | class JsonChangeStream implements ChangeStream |
||
30 | { |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $path; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $json; |
||
40 | |||
41 | /** |
||
42 | * @var JsonEncoder |
||
43 | */ |
||
44 | private $encoder; |
||
45 | |||
46 | /** |
||
47 | * @param string $path The path to the JSON file. |
||
48 | */ |
||
49 | 20 | public function __construct($path) |
|
50 | { |
||
51 | 20 | $this->path = $path; |
|
52 | 20 | $this->encoder = new JsonEncoder(); |
|
53 | 20 | $this->encoder->setPrettyPrinting(true); |
|
54 | 20 | $this->encoder->setEscapeSlash(false); |
|
55 | 20 | } |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 18 | public function append(PuliResource $resource) |
|
61 | { |
||
62 | 18 | if (null === $this->json) { |
|
63 | 17 | $this->load(); |
|
64 | } |
||
65 | |||
66 | 18 | if (!isset($this->json[$resource->getPath()])) { |
|
67 | 18 | $this->json[$resource->getPath()] = array(); |
|
68 | } |
||
69 | |||
70 | 18 | $this->json[$resource->getPath()][] = serialize($resource); |
|
71 | |||
72 | 18 | $this->flush(); |
|
73 | 18 | } |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 6 | public function purge($path) |
|
79 | { |
||
80 | 6 | if (null === $this->json) { |
|
81 | $this->load(); |
||
82 | } |
||
83 | |||
84 | 6 | unset($this->json[$path]); |
|
85 | |||
86 | 6 | $this->flush(); |
|
87 | 6 | } |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 2 | public function clear() |
|
93 | { |
||
94 | 2 | if (null === $this->json) { |
|
95 | $this->load(); |
||
96 | } |
||
97 | |||
98 | 2 | $this->json = array(); |
|
99 | |||
100 | 2 | $this->flush(); |
|
101 | 2 | } |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 8 | public function contains($path) |
|
107 | { |
||
108 | 8 | if (null === $this->json) { |
|
109 | 5 | $this->load(); |
|
110 | } |
||
111 | |||
112 | 8 | return isset($this->json[$path]); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 14 | public function getVersions($path, ResourceRepository $repository = null) |
|
119 | { |
||
120 | 14 | if (null === $this->json) { |
|
121 | 7 | $this->load(); |
|
122 | } |
||
123 | |||
124 | 14 | if (!isset($this->json[$path])) { |
|
125 | 4 | throw NoVersionFoundException::forPath($path); |
|
126 | } |
||
127 | |||
128 | 10 | $versions = array(); |
|
129 | |||
130 | 10 | View Code Duplication | foreach ($this->json[$path] as $resource) { |
|
|||
131 | 10 | $resource = unserialize($resource); |
|
132 | |||
133 | 10 | if (null !== $repository) { |
|
134 | 4 | $resource->attachTo($repository, $path); |
|
135 | } |
||
136 | |||
137 | 10 | $versions[] = $resource; |
|
138 | } |
||
139 | |||
140 | 10 | return new VersionList($path, $versions); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Loads the JSON file. |
||
145 | */ |
||
146 | 20 | private function load() |
|
147 | { |
||
148 | 20 | $decoder = new JsonDecoder(); |
|
149 | 20 | $decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY); |
|
150 | |||
151 | 20 | $this->json = file_exists($this->path) |
|
152 | 9 | ? $decoder->decodeFile($this->path) |
|
153 | 20 | : array(); |
|
154 | 20 | } |
|
155 | |||
156 | /** |
||
157 | * Writes the JSON file. |
||
158 | */ |
||
159 | 18 | private function flush() |
|
163 | } |
||
164 |
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.