Complex classes like FileAdapterStrategy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileAdapterStrategy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class FileAdapterStrategy extends AbstractAdapterStrategy |
||
| 65 | { |
||
| 66 | /** |
||
| 67 | * Adapter strategy type |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | const TYPE = 'file'; |
||
| 72 | /** |
||
| 73 | * Configuration |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $config = null; |
||
| 78 | /** |
||
| 79 | * Root directory (without trailing directory separator) |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $root = null; |
||
| 84 | /** |
||
| 85 | * Configuration directory (including trailing directory separator) |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $configDir = null; |
||
| 90 | /** |
||
| 91 | * Glob visibilities |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected static $globVisibilities = [ |
||
| 96 | SelectorInterface::VISIBLE => '', |
||
| 97 | SelectorInterface::HIDDEN => '.', |
||
| 98 | SelectorInterface::ALL => '{.,}', |
||
| 99 | ]; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Adapter strategy constructor |
||
| 103 | * |
||
| 104 | * @param array $config Adapter strategy configuration |
||
| 105 | * @throws InvalidArgumentException If the root directory configuration is empty |
||
| 106 | * @throws InvalidArgumentException If the root directory configuration is invalid |
||
| 107 | */ |
||
| 108 | 19 | public function __construct(array $config) |
|
| 109 | { |
||
| 110 | 19 | parent::__construct($config, ['root']); |
|
| 111 | |||
| 112 | // If the root directory configuration is empty |
||
| 113 | 18 | if (empty($this->config['root'])) { |
|
| 114 | 1 | throw new InvalidArgumentException( |
|
| 115 | 1 | 'Empty file adapter strategy root', |
|
| 116 | InvalidArgumentException::EMTPY_FILE_STRATEGY_ROOT |
||
| 117 | 1 | ); |
|
| 118 | } |
||
| 119 | |||
| 120 | // Get the real path of the root directory |
||
| 121 | 17 | $this->root = realpath($this->config['root']); |
|
| 122 | |||
| 123 | // If the repository should be initialized |
||
| 124 | 17 | if (!empty($this->config['init']) |
|
| 125 | 17 | && (boolean)$this->config['init'] |
|
| 126 | 17 | && $this->initializeRepository() |
|
| 127 | 15 | ) { |
|
| 128 | 5 | $this->root = realpath($this->config['root']); |
|
| 129 | 5 | } |
|
| 130 | |||
| 131 | // If the root directory configuration is still invalid |
||
| 132 | 15 | if (empty($this->root) || !@is_dir($this->root)) { |
|
| 133 | 1 | throw new InvalidArgumentException( |
|
| 134 | 1 | sprintf( |
|
| 135 | 1 | 'Invalid file adapter strategy root "%s"', |
|
| 136 | 1 | $this->config['root'] |
|
| 137 | 1 | ), |
|
| 138 | InvalidArgumentException::INVALID_FILE_STRATEGY_ROOT |
||
| 139 | 1 | ); |
|
| 140 | } |
||
| 141 | |||
| 142 | 14 | $this->configDir = $this->root.DIRECTORY_SEPARATOR.'.repo'.DIRECTORY_SEPARATOR; |
|
| 143 | 14 | } |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Initialize the repository |
||
| 147 | * |
||
| 148 | * @return boolean Success |
||
| 149 | * @throws RuntimeException If the repository cannot be initialized |
||
| 150 | * @throws RuntimeException If the repository size descriptor can not be created |
||
| 151 | */ |
||
| 152 | 7 | public function initializeRepository() |
|
| 153 | { |
||
| 154 | // Successively create the repository directories |
||
| 155 | 7 | $repoDirectories = [$this->config['root'], $this->config['root'].DIRECTORY_SEPARATOR.'.repo']; |
|
| 156 | 7 | foreach ($repoDirectories as $repoDirectory) { |
|
| 157 | // If the repository cannot be initialized |
||
| 158 | 7 | if (file_exists($repoDirectory) ? !is_dir($repoDirectory) : !mkdir($repoDirectory, 0777, true)) { |
|
| 159 | 1 | throw new RuntimeException('Could not initialize repository', RuntimeException::REPO_NOT_INITIALIZED); |
|
| 160 | } |
||
| 161 | 6 | } |
|
| 162 | |||
| 163 | // If the repository size descriptor can not be created |
||
| 164 | 6 | $configDir = $this->config['root'].DIRECTORY_SEPARATOR.'.repo'.DIRECTORY_SEPARATOR; |
|
| 165 | 6 | if ((file_exists($configDir.'size.txt') && !is_file($configDir.'size.txt')) |
|
| 166 | 5 | || !file_put_contents($configDir.'size.txt', '0') |
|
| 167 | 6 | ) { |
|
| 168 | 1 | throw new RuntimeException( |
|
| 169 | 1 | 'Could not create repository size descriptor', |
|
| 170 | RuntimeException::REPO_SIZE_DESCRIPTOR_NOT_CREATED |
||
| 171 | 1 | ); |
|
| 172 | } |
||
| 173 | |||
| 174 | 5 | return true; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Find objects by selector |
||
| 179 | * |
||
| 180 | * @param Selector|SelectorInterface $selector Object selector |
||
| 181 | * @param RepositoryInterface $repository Object repository |
||
| 182 | * @return PathInterface[] Object paths |
||
| 183 | */ |
||
| 184 | 7 | public function findObjectPaths(SelectorInterface $selector, RepositoryInterface $repository) |
|
| 185 | { |
||
| 186 | 7 | chdir($this->root); |
|
| 187 | |||
| 188 | // Build a glob string from the selector |
||
| 189 | 7 | $glob = ''; |
|
| 190 | 7 | $globFlags = GLOB_ONLYDIR | GLOB_NOSORT; |
|
| 191 | |||
| 192 | 7 | $year = $selector->getYear(); |
|
| 193 | 7 | if ($year !== null) { |
|
| 194 | 7 | $glob .= '/'.$year; |
|
| 195 | 7 | } |
|
| 196 | |||
| 197 | 7 | $month = $selector->getMonth(); |
|
| 198 | 7 | if ($month !== null) { |
|
| 199 | 7 | $glob .= '/'.$month; |
|
| 200 | 7 | } |
|
| 201 | |||
| 202 | 7 | $day = $selector->getDay(); |
|
| 203 | 7 | if ($day !== null) { |
|
| 204 | 7 | $glob .= '/'.$day; |
|
| 205 | 7 | } |
|
| 206 | |||
| 207 | 7 | $hour = $selector->getHour(); |
|
| 208 | 7 | if ($hour !== null) { |
|
| 209 | 2 | $glob .= '/'.$hour; |
|
| 210 | 2 | } |
|
| 211 | |||
| 212 | 7 | $minute = $selector->getMinute(); |
|
| 213 | 7 | if ($minute !== null) { |
|
| 214 | 2 | $glob .= '/'.$minute; |
|
| 215 | 2 | } |
|
| 216 | |||
| 217 | 7 | $second = $selector->getSecond(); |
|
| 218 | 7 | if ($second !== null) { |
|
| 219 | 2 | $glob .= '/'.$second; |
|
| 220 | 2 | } |
|
| 221 | |||
| 222 | 7 | $visibility = $selector->getVisibility(); |
|
| 223 | 7 | $uid = $selector->getId(); |
|
| 224 | 7 | $type = $selector->getType(); |
|
| 225 | 7 | if (($uid !== null) || ($type !== null)) { |
|
| 226 | 7 | $glob .= '/'.($uid ?: SelectorInterface::WILDCARD).'-'.($type ?: SelectorInterface::WILDCARD); |
|
| 227 | |||
| 228 | 7 | $revision = $selector->getRevision(); |
|
| 229 | 7 | if ($revision !== null) { |
|
| 230 | 1 | $glob .= '/'.self::$globVisibilities[$visibility].($uid ?: SelectorInterface::WILDCARD).'-'.$revision; |
|
| 231 | 1 | $globFlags &= ~GLOB_ONLYDIR; |
|
| 232 | 1 | } |
|
| 233 | 7 | } |
|
| 234 | |||
| 235 | 7 | return array_map( |
|
| 236 | 7 | function ($objectPath) use ($repository) { |
|
| 237 | 7 | return Kernel::create(RepositoryPath::class, [$repository, '/'.$objectPath]); |
|
| 238 | 7 | }, |
|
| 239 | 7 | glob(ltrim($glob, '/'), $globFlags) |
|
| 240 | 7 | ); |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Test if an object resource exists |
||
| 245 | * |
||
| 246 | * @param string $resourcePath Repository relative resource path |
||
| 247 | * @return boolean Object resource exists |
||
| 248 | */ |
||
| 249 | 27 | public function hasObjectResource($resourcePath) |
|
| 250 | { |
||
| 251 | 27 | return is_file($this->root.$resourcePath); |
|
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Find and return an object resource |
||
| 256 | * |
||
| 257 | * @param string $resourcePath Repository relative resource path |
||
| 258 | * @return ResourceInterface Object resource |
||
| 259 | */ |
||
| 260 | 26 | public function getObjectResource($resourcePath) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Allocate an object ID and create an object resource |
||
| 267 | * |
||
| 268 | * @param \Closure $creator Object creation closure |
||
| 269 | * @return ObjectInterface Object |
||
| 270 | * @throws RuntimeException If no object could be created |
||
| 271 | * @throws \Exception If another error occurs |
||
| 272 | */ |
||
| 273 | 4 | public function createObjectResource(\Closure $creator) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Persist an object in the repository |
||
| 327 | * |
||
| 328 | * @param ObjectInterface $object Object |
||
| 329 | * @return AdapterStrategyInterface Self reference |
||
| 330 | */ |
||
| 331 | 3 | public function persistObject(ObjectInterface $object) |
|
| 332 | { |
||
| 333 | // If the object has just been deleted |
||
| 334 | 3 | if ($object->hasBeenDeleted()) { |
|
| 335 | 3 | return $this->deleteObject($object); |
|
| 336 | |||
| 337 | // Elseif the object has just been undeleted |
||
| 338 | 3 | } elseif ($object->hasBeenUndeleted()) { |
|
| 339 | 2 | return $this->undeleteObject($object); |
|
| 340 | |||
| 341 | // If the object has just been published |
||
| 342 | 3 | } elseif ($object->hasBeenPublished()) { |
|
| 343 | 1 | $this->publishObject($object); |
|
| 344 | 1 | } |
|
| 345 | |||
| 346 | // Persist the object resource |
||
| 347 | 3 | return $this->persistObjectResource($object); |
|
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Publish an object in the repository |
||
| 352 | * |
||
| 353 | * @param ObjectInterface $object |
||
| 354 | */ |
||
| 355 | 1 | protected function publishObject(ObjectInterface $object) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Build an absolute repository resource path |
||
| 389 | * |
||
| 390 | * @param RepositoryPathInterface $repositoryPath Repository path |
||
| 391 | * @return string Absolute repository resource path |
||
| 392 | */ |
||
| 393 | 3 | protected function absoluteResourcePath(RepositoryPathInterface $repositoryPath) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Persist an object resource in the repository |
||
| 404 | * |
||
| 405 | * @param ObjectInterface $object Object |
||
| 406 | * @return AdapterStrategyInterface Self reference |
||
| 407 | */ |
||
| 408 | 3 | protected function persistObjectResource(ObjectInterface $object) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Return the repository size (number of objects in the repository) |
||
| 428 | * |
||
| 429 | * @return int Repository size |
||
| 430 | */ |
||
| 431 | 4 | public function getRepositorySize() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Delete all revisions of an object |
||
| 443 | * |
||
| 444 | * @param ObjectInterface $object Object |
||
| 445 | * @return ObjectInterface Object |
||
| 446 | */ |
||
| 447 | 3 | protected function deleteObject(ObjectInterface $object) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Undelete all revisions of an object |
||
| 475 | * |
||
| 476 | * @param ObjectInterface $object Object |
||
| 477 | * @return ObjectInterface Object |
||
| 478 | */ |
||
| 479 | 2 | protected function undeleteObject(ObjectInterface $object) |
|
| 504 | } |
||
| 505 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: