| Total Complexity | 40 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractStrictWikiTemplate 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.
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 AbstractStrictWikiTemplate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class AbstractStrictWikiTemplate extends AbstractParametersObject implements WikiTemplateInterface |
||
| 17 | { |
||
| 18 | use ArrayProcessTrait, InfoTrait; |
||
| 19 | /** |
||
| 20 | * Name of the wiki-template |
||
| 21 | */ |
||
| 22 | const WIKITEMPLATE_NAME = 'NO NAME'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Error in wiki parsing without those required params. |
||
| 26 | */ |
||
| 27 | const REQUIRED_PARAMETERS = []; |
||
| 28 | /** |
||
| 29 | * The minimum parameters for pretty wiki-template. |
||
| 30 | */ |
||
| 31 | const MINIMUM_PARAMETERS = []; |
||
| 32 | |||
| 33 | /* commented to allow inherit from Interface in OuvrageTemplate |
||
| 34 | const PARAM_ALIAS = []; */ |
||
| 35 | |||
| 36 | const COMMENT_STRIPPED = '<!-- Paramètre obligatoire -->'; |
||
| 37 | |||
| 38 | public $log = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * AbstractWikiTemplate constructor. |
||
| 42 | * |
||
| 43 | * @throws Exception |
||
| 44 | */ |
||
| 45 | public function __construct() |
||
| 46 | { |
||
| 47 | if (empty(static::MINIMUM_PARAMETERS)) { |
||
|
|
|||
| 48 | throw new Exception(sprintf('DEFAULT_PARAMETERS not configured in "%s"', get_called_class())); |
||
| 49 | } |
||
| 50 | $this->parametersValues = static::MINIMUM_PARAMETERS; |
||
| 51 | |||
| 52 | if (empty($this->parametersByOrder)) { |
||
| 53 | $this->parametersByOrder = static::MINIMUM_PARAMETERS; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | public function getParamsAndAlias(): array |
||
| 58 | { |
||
| 59 | return array_merge($this->parametersByOrder, array_keys(static::PARAM_ALIAS)); |
||
|
1 ignored issue
–
show
|
|||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array $data |
||
| 64 | * |
||
| 65 | * @return AbstractStrictWikiTemplate |
||
| 66 | * @throws Exception |
||
| 67 | */ |
||
| 68 | public function hydrate(array $data): AbstractStrictWikiTemplate |
||
| 69 | { |
||
| 70 | foreach ($data as $name => $value) { |
||
| 71 | if (is_string($value)) { |
||
| 72 | $this->hydrateTemplateParameter($name, $value); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param $name |
||
| 81 | * @param string $value |
||
| 82 | * |
||
| 83 | * @throws Exception |
||
| 84 | */ |
||
| 85 | protected function hydrateTemplateParameter($name, string $value): void |
||
| 86 | { |
||
| 87 | if ($this->isValidParamName($name)) { |
||
| 88 | $this->setParam($name, $value); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Delete key if empty value and the key not required. |
||
| 94 | * |
||
| 95 | * @param array $params |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | protected function keepMinimumOrNotEmpty(array $params): array |
||
| 100 | { |
||
| 101 | $render = []; |
||
| 102 | foreach ($params as $name => $value) { |
||
| 103 | if (empty($value) && !isset(static::MINIMUM_PARAMETERS[$name])) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | $render[$name] = $params[$name]; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $render; |
||
| 110 | } |
||
| 111 | |||
| 112 | protected function paramsByRenderOrder(): array |
||
| 113 | { |
||
| 114 | $renderParams = []; |
||
| 115 | |||
| 116 | // default order |
||
| 117 | foreach ($this->parametersByOrder as $order => $paramName) { |
||
| 118 | if (isset($this->parametersValues[$paramName])) { |
||
| 119 | $renderParams[$paramName] = $this->parametersValues[$paramName]; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | return $renderParams; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $name |
||
| 128 | * |
||
| 129 | * @return string|null |
||
| 130 | * @throws Exception |
||
| 131 | */ |
||
| 132 | public function getParam(string $name): ?string |
||
| 133 | { |
||
| 134 | if (!$this->isValidParamName($name)) { |
||
| 135 | return null; |
||
| 136 | } |
||
| 137 | $name = $this->getAliasParam($name); |
||
| 138 | // keyNum parameter ? |
||
| 139 | // if (!in_array($name, ['1', '2', '3', '4'])) { |
||
| 140 | |||
| 141 | return ($this->parametersValues[$name]) ?? null; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Todo replacement for old validOrExceptionOnParamName() |
||
| 146 | * |
||
| 147 | * @param $name |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | protected function isValidParamName($name): bool |
||
| 152 | { |
||
| 153 | if (is_int($name)) { |
||
| 154 | $name = (string)$name; |
||
| 155 | } |
||
| 156 | |||
| 157 | // that parameter exists in template ? |
||
| 158 | if (in_array($name, $this->parametersByOrder) |
||
| 159 | || array_key_exists($name, static::PARAM_ALIAS) |
||
| 160 | ) { |
||
| 161 | return true; |
||
| 162 | } |
||
| 163 | |||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $name |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | * @throws Exception |
||
| 172 | */ |
||
| 173 | public function unsetParam(string $name) |
||
| 174 | { |
||
| 175 | if (!$this->isValidParamName($name)) { |
||
| 176 | throw new Exception(sprintf('no parameter "%s" in template "%s"', $name, get_called_class())); |
||
| 177 | } |
||
| 178 | $name = $this->getAliasParam($name); |
||
| 179 | unset($this->parametersValues[$name]); |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * TODO ? check if method set{ParamName} exists ? |
||
| 186 | * |
||
| 187 | * @param string $name |
||
| 188 | * @param string $value |
||
| 189 | * |
||
| 190 | * @return AbstractParametersObject |
||
| 191 | * @throws Exception |
||
| 192 | */ |
||
| 193 | public function setParam(string $name, string $value): AbstractParametersObject |
||
| 194 | { |
||
| 195 | if (!$this->isValidParamName($name)) { |
||
| 196 | $this->log[] = sprintf('no parameter "%s" in AbstractParametersObject "%s"', $name, get_called_class()); |
||
| 197 | |||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | $name = $this->getAliasParam($name); |
||
| 202 | $value = trim($value); |
||
| 203 | if (!empty($value) || $this->parametersValues[$name]) { |
||
| 204 | $this->parametersValues[$name] = $value; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * todo peu utile en public |
||
| 212 | * |
||
| 213 | * @param string $name |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getAliasParam(string $name): string |
||
| 218 | { |
||
| 219 | if (array_key_exists($name, static::PARAM_ALIAS)) { |
||
| 220 | $name = static::PARAM_ALIAS[$name]; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $name; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * For a parameter, check is the value exists (not empty). |
||
| 228 | * |
||
| 229 | * @param string $name |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | * @throws Exception |
||
| 233 | */ |
||
| 234 | public function hasParamValue(string $name): bool |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Verify the required template parameters for an edit by the bot. |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | * @throws Exception |
||
| 252 | * @throws Exception |
||
| 253 | * @throws Exception |
||
| 254 | * @throws Exception |
||
| 255 | */ |
||
| 256 | public function isValidForEdit(): bool |
||
| 276 | } |
||
| 277 | |||
| 278 | public abstract function serialize(?bool $cleanOrder = false): string; |
||
| 279 | |||
| 280 | } |
||
| 281 |