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 |
||
39 | class Factory implements ICacheFactory { |
||
40 | const NULL_CACHE = NullCache::class; |
||
41 | |||
42 | /** |
||
43 | * @var string $globalPrefix |
||
44 | */ |
||
45 | private $globalPrefix; |
||
46 | |||
47 | /** |
||
48 | * @var ILogger $logger |
||
49 | */ |
||
50 | private $logger; |
||
51 | |||
52 | /** |
||
53 | * @var string $localCacheClass |
||
54 | */ |
||
55 | private $localCacheClass; |
||
56 | |||
57 | /** |
||
58 | * @var string $distributedCacheClass |
||
59 | */ |
||
60 | private $distributedCacheClass; |
||
61 | |||
62 | /** |
||
63 | * @var string $lockingCacheClass |
||
64 | */ |
||
65 | private $lockingCacheClass; |
||
66 | |||
67 | /** |
||
68 | * @param string $globalPrefix |
||
69 | * @param ILogger $logger |
||
70 | * @param string|null $localCacheClass |
||
71 | * @param string|null $distributedCacheClass |
||
72 | * @param string|null $lockingCacheClass |
||
73 | */ |
||
74 | public function __construct(string $globalPrefix, ILogger $logger, |
||
75 | $localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null) |
||
76 | { |
||
77 | $this->logger = $logger; |
||
78 | $this->globalPrefix = $globalPrefix; |
||
79 | |||
80 | if (!$localCacheClass) { |
||
|
|||
81 | $localCacheClass = self::NULL_CACHE; |
||
82 | } |
||
83 | if (!$distributedCacheClass) { |
||
84 | $distributedCacheClass = $localCacheClass; |
||
85 | } |
||
86 | |||
87 | $missingCacheMessage = 'Memcache {class} not available for {use} cache'; |
||
88 | $missingCacheHint = 'Is the matching PHP module installed and enabled?'; |
||
89 | View Code Duplication | if (!class_exists($localCacheClass) || !$localCacheClass::isAvailable()) { |
|
90 | if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
||
91 | // CLI should not hard-fail on broken memcache |
||
92 | $this->logger->info($missingCacheMessage, [ |
||
93 | 'class' => $localCacheClass, |
||
94 | 'use' => 'local', |
||
95 | 'app' => 'cli' |
||
96 | ]); |
||
97 | $localCacheClass = self::NULL_CACHE; |
||
98 | } else { |
||
99 | throw new \OC\HintException(strtr($missingCacheMessage, [ |
||
100 | '{class}' => $localCacheClass, '{use}' => 'local' |
||
101 | ]), $missingCacheHint); |
||
102 | } |
||
103 | } |
||
104 | View Code Duplication | if (!class_exists($distributedCacheClass) || !$distributedCacheClass::isAvailable()) { |
|
105 | if (\OC::$CLI && !defined('PHPUNIT_RUN')) { |
||
106 | // CLI should not hard-fail on broken memcache |
||
107 | $this->logger->info($missingCacheMessage, [ |
||
108 | 'class' => $distributedCacheClass, |
||
109 | 'use' => 'distributed', |
||
110 | 'app' => 'cli' |
||
111 | ]); |
||
112 | $distributedCacheClass = self::NULL_CACHE; |
||
113 | } else { |
||
114 | throw new \OC\HintException(strtr($missingCacheMessage, [ |
||
115 | '{class}' => $distributedCacheClass, '{use}' => 'distributed' |
||
116 | ]), $missingCacheHint); |
||
117 | } |
||
118 | } |
||
119 | if (!($lockingCacheClass && class_exists($distributedCacheClass) && $lockingCacheClass::isAvailable())) { |
||
120 | // don't fallback since the fallback might not be suitable for storing lock |
||
121 | $lockingCacheClass = self::NULL_CACHE; |
||
122 | } |
||
123 | |||
124 | $this->localCacheClass = $localCacheClass; |
||
125 | $this->distributedCacheClass = $distributedCacheClass; |
||
126 | $this->lockingCacheClass = $lockingCacheClass; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * create a cache instance for storing locks |
||
131 | * |
||
132 | * @param string $prefix |
||
133 | * @return IMemcache |
||
134 | */ |
||
135 | public function createLocking(string $prefix = ''): IMemcache { |
||
136 | return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * create a distributed cache instance |
||
141 | * |
||
142 | * @param string $prefix |
||
143 | * @return ICache |
||
144 | */ |
||
145 | public function createDistributed(string $prefix = ''): ICache { |
||
146 | return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * create a local cache instance |
||
151 | * |
||
152 | * @param string $prefix |
||
153 | * @return ICache |
||
154 | */ |
||
155 | public function createLocal(string $prefix = ''): ICache { |
||
156 | return new $this->localCacheClass($this->globalPrefix . '/' . $prefix); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @see \OC\Memcache\Factory::createDistributed() |
||
161 | * @param string $prefix |
||
162 | * @return ICache |
||
163 | * @deprecated 13.0.0 Use either createLocking, createDistributed or createLocal |
||
164 | */ |
||
165 | public function create(string $prefix = ''): ICache { |
||
168 | |||
169 | /** |
||
170 | * check memcache availability |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function isAvailable(): bool { |
||
177 | |||
178 | /** |
||
179 | * @see \OC\Memcache\Factory::createLocal() |
||
180 | * @param string $prefix |
||
181 | * @return ICache |
||
182 | */ |
||
183 | public function createLowLatency(string $prefix = ''): ICache { |
||
186 | |||
187 | /** |
||
188 | * Check if a local memory cache backend is available |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function isLocalCacheAvailable(): bool { |
||
195 | } |
||
196 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: