puli /
manager
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the puli/manager Module. |
||
| 5 | * |
||
| 6 | * (c) Bernhard Schussek <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Puli\Manager\Api\Cache; |
||
| 13 | |||
| 14 | use InvalidArgumentException; |
||
| 15 | use Puli\Manager\Api\Module\InstallInfo; |
||
| 16 | use Puli\Manager\Api\Module\ModuleFile; |
||
| 17 | use Puli\Manager\Assert\Assert; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Stores combined Modules configuration. |
||
| 21 | * |
||
| 22 | * @since 1.0 |
||
| 23 | * |
||
| 24 | * @author Mateusz Sojda <[email protected]> |
||
| 25 | */ |
||
| 26 | class CacheFile |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var string|null |
||
| 30 | */ |
||
| 31 | private $path; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ModuleFile[] |
||
| 35 | */ |
||
| 36 | private $moduleFiles = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var InstallInfo[] |
||
| 40 | */ |
||
| 41 | private $installInfos = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Creates new CacheFile. |
||
| 45 | * |
||
| 46 | * @param string|null $path The path where the cache file is stored. |
||
| 47 | */ |
||
| 48 | 15 | public function __construct($path = null) |
|
| 49 | { |
||
| 50 | 15 | Assert::nullOrAbsoluteSystemPath($path); |
|
| 51 | |||
| 52 | 13 | $this->path = $path; |
|
| 53 | 13 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Returns the path to the cache file. |
||
| 57 | * |
||
| 58 | * @return string|null The path or `null` if this file is not stored on the |
||
| 59 | * file system. |
||
| 60 | */ |
||
| 61 | 2 | public function getPath() |
|
| 62 | { |
||
| 63 | 2 | return $this->path; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Sets the Module files. |
||
| 68 | * |
||
| 69 | * @param ModuleFile[] $moduleFiles The Module files. |
||
| 70 | */ |
||
| 71 | public function setModuleFiles(array $moduleFiles) |
||
| 72 | { |
||
| 73 | $this->moduleFiles = array(); |
||
| 74 | |||
| 75 | foreach ($moduleFiles as $moduleFile) { |
||
| 76 | $this->addModuleFile($moduleFile); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Adds a Module to the cache file. |
||
| 82 | * |
||
| 83 | * @param ModuleFile $moduleFile The added Module file. |
||
| 84 | */ |
||
| 85 | public function addModuleFile(ModuleFile $moduleFile) |
||
| 86 | { |
||
| 87 | $this->moduleFiles[$moduleFile->getModuleName()] = $moduleFile; |
||
| 88 | |||
| 89 | ksort($this->moduleFiles); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Removes a Module file from the cache file. |
||
| 94 | * |
||
| 95 | * @param string $moduleName The Module name. |
||
| 96 | */ |
||
| 97 | public function removeModuleFile($moduleName) |
||
| 98 | { |
||
| 99 | unset($this->moduleFiles[$moduleName]); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Removes all Module files from the cache file. |
||
| 104 | */ |
||
| 105 | public function clearModuleFiles() |
||
| 106 | { |
||
| 107 | $this->moduleFiles = array(); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the Module file with the given name. |
||
| 112 | * |
||
| 113 | * @param string $moduleName The Module name. |
||
| 114 | * |
||
| 115 | * @return ModuleFile The Module with the passed name. |
||
| 116 | * |
||
| 117 | * @throws InvalidArgumentException If the Module was not found. |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function getModuleFile($moduleName) |
|
|
0 ignored issues
–
show
|
|||
| 120 | { |
||
| 121 | Assert::ModuleName($moduleName); |
||
| 122 | |||
| 123 | if (!isset($this->moduleFiles[$moduleName])) { |
||
| 124 | throw new InvalidArgumentException(sprintf("Could not find a Module named %s.", $moduleName)); |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
Could not find a Module named %s. does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. Loading history...
|
|||
| 125 | } |
||
| 126 | |||
| 127 | return $this->moduleFiles[$moduleName]; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the Module files in the cache file. |
||
| 132 | * |
||
| 133 | * @return ModuleFile[] The Module files in the cache file. |
||
| 134 | */ |
||
| 135 | public function getModuleFiles() |
||
| 136 | { |
||
| 137 | return $this->moduleFiles; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns whether a Module with the given name exists. |
||
| 142 | * |
||
| 143 | * @param string $moduleName The Module name. |
||
| 144 | * |
||
| 145 | * @return bool Whether a Module with this name exists. |
||
| 146 | */ |
||
| 147 | public function hasModuleFile($moduleName) |
||
| 148 | { |
||
| 149 | return isset($this->moduleFiles[$moduleName]); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns whether a cache file contains any Module files. |
||
| 154 | * |
||
| 155 | * @return bool Whether a cache file contains any Module files. |
||
| 156 | */ |
||
| 157 | public function hasModuleFiles() |
||
| 158 | { |
||
| 159 | return count($this->moduleFiles) > 0; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Adds install info to the cache file. |
||
| 164 | * |
||
| 165 | * @param InstallInfo $installInfo The Module install info. |
||
| 166 | */ |
||
| 167 | public function addInstallInfo(InstallInfo $installInfo) |
||
| 168 | { |
||
| 169 | $this->installInfos[$installInfo->getModuleName()] = $installInfo; |
||
| 170 | |||
| 171 | ksort($this->installInfos); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Removes install info file from the cache file. |
||
| 176 | * |
||
| 177 | * @param string $moduleName The Module name. |
||
| 178 | */ |
||
| 179 | public function removeInstallInfo($moduleName) |
||
| 180 | { |
||
| 181 | unset($this->installInfos[$moduleName]); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Removes all Module install info from the cache file. |
||
| 186 | */ |
||
| 187 | public function clearInstallInfo() |
||
| 188 | { |
||
| 189 | $this->installInfos = array(); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the install info with the given Module name. |
||
| 194 | * |
||
| 195 | * @param string $moduleName The Module name. |
||
| 196 | * |
||
| 197 | * @return InstallInfo The Module install info with the passed name. |
||
| 198 | * |
||
| 199 | * @throws InvalidArgumentException If the install info was not found. |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function getInstallInfo($moduleName) |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 202 | { |
||
| 203 | Assert::ModuleName($moduleName); |
||
| 204 | |||
| 205 | if (!isset($this->installInfos[$moduleName])) { |
||
| 206 | throw new InvalidArgumentException(sprintf("Could not find a Module named %s.", $moduleName)); |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
The string literal
Could not find a Module named %s. does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. Loading history...
|
|||
| 207 | } |
||
| 208 | |||
| 209 | return $this->installInfos[$moduleName]; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns the install info for all Modules in the cache file. |
||
| 214 | * |
||
| 215 | * @return InstallInfo[] The install info for all Modules in the cache file. |
||
| 216 | */ |
||
| 217 | public function getInstallInfos() |
||
| 218 | { |
||
| 219 | return $this->installInfos; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns whether install info for a Module with the given name exists. |
||
| 224 | * |
||
| 225 | * @param string $moduleName The Module name. |
||
| 226 | * |
||
| 227 | * @return bool Whether install info for a Module with this name exists. |
||
| 228 | */ |
||
| 229 | public function hasInstallInfo($moduleName) |
||
| 230 | { |
||
| 231 | return isset($this->installInfos[$moduleName]); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns whether a cache file contains any Module install info. |
||
| 236 | * |
||
| 237 | * @return bool Whether a cache file contains any Module install info. |
||
| 238 | */ |
||
| 239 | public function hasInstallInfos() |
||
| 240 | { |
||
| 241 | return count($this->installInfos) > 0; |
||
| 242 | } |
||
| 243 | } |
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.