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 |
||
17 | trait Base64FileTrait |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $path; |
||
23 | |||
24 | /** |
||
25 | * @var resource |
||
26 | */ |
||
27 | private $resource; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 318 | public function __destruct() |
|
33 | { |
||
34 | 318 | if (is_resource($this->resource)) { |
|
35 | 318 | fclose($this->resource); |
|
36 | 248 | } |
|
37 | |||
38 | 318 | if (file_exists($this->path)) { |
|
39 | 318 | unlink($this->path); |
|
40 | 248 | } |
|
41 | 318 | } |
|
42 | |||
43 | /** |
||
44 | * @param bool $encoded |
||
45 | * @param bool $asResource |
||
46 | * |
||
47 | * @return resource|string |
||
48 | */ |
||
49 | 315 | public function getData($encoded = true, $asResource = true) |
|
50 | { |
||
51 | 315 | $resource = fopen('php://temp', 'rb+'); |
|
52 | |||
53 | 315 | if ($encoded) { |
|
54 | 171 | $filter = stream_filter_append($resource, 'convert.base64-encode', STREAM_FILTER_WRITE); |
|
55 | 133 | } |
|
56 | |||
57 | 315 | $this->copyStreamToStream($this->resource, $resource); |
|
58 | |||
59 | 315 | if (isset($filter)) { |
|
60 | 171 | stream_filter_remove($filter); |
|
61 | 133 | } |
|
62 | |||
63 | 315 | if ($asResource) { |
|
64 | 144 | return $resource; |
|
65 | } |
||
66 | |||
67 | 171 | $content = stream_get_contents($resource); |
|
68 | 171 | fclose($resource); |
|
69 | |||
70 | 171 | return $content; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param string|resource $value |
||
75 | * @param bool $encoded |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 405 | private function load($value, $encoded = true) |
|
80 | { |
||
81 | 405 | $this->path = tempnam(sys_get_temp_dir(), 'ivory_base64'); |
|
82 | 405 | $this->resource = fopen($this->path, 'w+'); |
|
83 | |||
84 | 405 | if ($encoded) { |
|
85 | 261 | $filter = stream_filter_append($this->resource, 'convert.base64-decode', STREAM_FILTER_WRITE); |
|
86 | 203 | } |
|
87 | |||
88 | try { |
||
89 | 405 | if (is_string($value)) { |
|
90 | 198 | $this->copyStringToStream($value, $this->resource); |
|
91 | 340 | } elseif (is_resource($value)) { |
|
92 | 189 | $this->copyStreamToStream($value, $this->resource); |
|
93 | 133 | } else { |
|
94 | 18 | throw new \InvalidArgumentException(sprintf( |
|
95 | 18 | 'The base64 file value must be a string or a resource, got "%s".', |
|
96 | 94 | is_object($value) ? get_class($value) : gettype($value) |
|
97 | 14 | )); |
|
98 | } |
||
99 | 329 | } catch (\Exception $e) { |
|
100 | 63 | fclose($this->resource); |
|
101 | |||
102 | 63 | throw $e; |
|
103 | } |
||
104 | |||
105 | 342 | if (isset($filter)) { |
|
106 | 198 | stream_filter_remove($filter); |
|
107 | 154 | } |
|
108 | |||
109 | 342 | fflush($this->resource); |
|
110 | |||
111 | 342 | return $this->path; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param string $from |
||
116 | * @param resource $to |
||
117 | */ |
||
118 | 198 | private function copyStringToStream($from, $to) |
|
133 | |||
134 | /** |
||
135 | * @param resource $from |
||
136 | * @param resource $to |
||
137 | */ |
||
138 | 360 | private function copyStreamToStream($from, $to) |
|
167 | } |
||
168 |
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.