| Total Complexity | 52 |
| Total Lines | 521 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CreateFile 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 CreateFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace XoopsModules\Tdmcreate\Files; |
||
| 30 | class CreateFile extends CreateTableFields |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $xf = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * "fileName" attribute of the files. |
||
| 39 | * |
||
| 40 | * @var mixed |
||
| 41 | */ |
||
| 42 | private $fileName = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * "subdir" attribute of the directories. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $subdir = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * "uploadPath" attribute of the files. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $uploadPath = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $content = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var mixed |
||
| 65 | */ |
||
| 66 | private $created = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var mixed |
||
| 70 | */ |
||
| 71 | private $notCreated = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $mode = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $phpcode = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $htmlcode; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @public function constructor |
||
| 90 | * @param null |
||
| 91 | */ |
||
| 92 | public function __construct() |
||
| 93 | { |
||
| 94 | parent::__construct(); |
||
| 95 | $this->xf = \XoopsFile::getHandler(); |
||
|
|
|||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @public static function getInstance |
||
| 100 | * @param null |
||
| 101 | * @return Tdmcreate\Files\CreateFile |
||
| 102 | */ |
||
| 103 | public static function getInstance() |
||
| 104 | { |
||
| 105 | static $instance = false; |
||
| 106 | if (!$instance) { |
||
| 107 | $instance = new self(); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $instance; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @public function create |
||
| 115 | * |
||
| 116 | * @param $moduleDirname |
||
| 117 | * @param $subdir |
||
| 118 | * @param $fileName |
||
| 119 | * @param $content |
||
| 120 | * @param $created |
||
| 121 | * @param $notCreated |
||
| 122 | * @param $mode |
||
| 123 | */ |
||
| 124 | public function create($moduleDirname, $subdir = null, $fileName = null, $content = '', $created = null, $notCreated = null, $mode = 'w+') |
||
| 125 | { |
||
| 126 | $this->setFileName($fileName); |
||
| 127 | $this->created = $created; |
||
| 128 | $this->notCreated = $notCreated; |
||
| 129 | $this->setMode($mode); |
||
| 130 | $this->setRepositoryPath($moduleDirname); |
||
| 131 | if (!empty($content) && is_string($content)) { |
||
| 132 | $this->setContent($content); |
||
| 133 | } |
||
| 134 | if (isset($subdir) && is_string($subdir)) { |
||
| 135 | $this->setSubDir($subdir); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @public function write |
||
| 141 | * @param string $module |
||
| 142 | * @param string $fileName |
||
| 143 | * @param mixed $moduleDirname |
||
| 144 | */ |
||
| 145 | /*public function write($module, $fileName) |
||
| 146 | { |
||
| 147 | $this->setModule($module); |
||
| 148 | $this->setFileName($fileName); |
||
| 149 | }*/ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @private function setRepositoryPath |
||
| 153 | * @param string $moduleDirname |
||
| 154 | */ |
||
| 155 | private function setRepositoryPath($moduleDirname) |
||
| 156 | { |
||
| 157 | $this->uploadPath = TDMC_UPLOAD_REPOSITORY_PATH . '/' . $moduleDirname; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @private function getRepositoryPath |
||
| 162 | * @param null |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | private function getRepositoryPath() |
||
| 166 | { |
||
| 167 | return $this->uploadPath; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @private function setSubDir |
||
| 172 | * @param $subdir |
||
| 173 | */ |
||
| 174 | private function setSubDir($subdir) |
||
| 175 | { |
||
| 176 | $this->subdir = $subdir; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @private function getSubDir |
||
| 181 | * @param null |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | private function getSubDir() |
||
| 185 | { |
||
| 186 | return $this->subdir; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * public function setFileName. |
||
| 191 | * |
||
| 192 | * @param $fileName |
||
| 193 | */ |
||
| 194 | public function setFileName($fileName) |
||
| 195 | { |
||
| 196 | $this->fileName = $fileName; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @public function getFileName |
||
| 201 | * @param null |
||
| 202 | * @return mixed |
||
| 203 | */ |
||
| 204 | public function getFileName() |
||
| 205 | { |
||
| 206 | return $this->fileName; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @private function setContent |
||
| 211 | * @param $content |
||
| 212 | */ |
||
| 213 | private function setContent($content) |
||
| 214 | { |
||
| 215 | $this->content = $content; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @private function setContent |
||
| 220 | * @param null |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | private function getContent() |
||
| 224 | { |
||
| 225 | return $this->content; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @private function getFolderName |
||
| 230 | * @param null |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | private function getFolderName() |
||
| 234 | { |
||
| 235 | $path = $this->getUploadPath(); |
||
| 236 | if (mb_strrpos($path, '\\')) { |
||
| 237 | $str = mb_strrpos($path, '\\'); |
||
| 238 | if (false !== $str) { |
||
| 239 | return mb_substr($path, $str + 1, mb_strlen($path)); |
||
| 240 | } |
||
| 241 | |||
| 242 | return mb_substr($path, $str, mb_strlen($path)); |
||
| 243 | } |
||
| 244 | //return 'root module'; |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @public function getUploadPath |
||
| 250 | * @param null |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | private function getUploadPath() |
||
| 254 | { |
||
| 255 | if (null != $this->getSubDir()) { |
||
| 256 | $ret = $this->getRepositoryPath() . '/' . $this->getSubDir(); |
||
| 257 | } else { |
||
| 258 | $ret = $this->getRepositoryPath(); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $ret; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @private function getCreated |
||
| 266 | * @param null |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | private function getCreated() |
||
| 270 | { |
||
| 271 | return $this->created; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @private function getNotCreated |
||
| 276 | * @param null |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | private function getNotCreated() |
||
| 280 | { |
||
| 281 | return $this->notCreated; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @private function setMode |
||
| 286 | * @param $mode |
||
| 287 | */ |
||
| 288 | private function setMode($mode) |
||
| 289 | { |
||
| 290 | $this->mode = $mode; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @private function getMode |
||
| 295 | * @param null |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | private function getMode() |
||
| 299 | { |
||
| 300 | return $this->mode; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @public function getLanguage |
||
| 305 | * @param string $moduleDirname |
||
| 306 | * @param string $prefix |
||
| 307 | * @param string $suffix |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getLanguage($moduleDirname, $prefix = '', $suffix = '') |
||
| 312 | { |
||
| 313 | $lang = '_' . $prefix . '_' . mb_strtoupper($moduleDirname); |
||
| 314 | $ret = $lang; |
||
| 315 | if (!empty($suffix) || '_' !== $suffix) { |
||
| 316 | $ret = $lang . '_' . $suffix; |
||
| 317 | } elseif ('_' === $suffix) { |
||
| 318 | $ret = $lang . '_'; |
||
| 319 | } |
||
| 320 | |||
| 321 | return $ret; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @public function getLeftString |
||
| 326 | * @param string $string |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getLeftString($string) |
||
| 331 | { |
||
| 332 | return mb_substr($string, 0, mb_strpos($string, '_')); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @public function getRightString |
||
| 337 | * @param $string |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getRightString($string = null) |
||
| 342 | { |
||
| 343 | if (mb_strpos($string, '_')) { |
||
| 344 | $str = mb_strpos($string, '_'); |
||
| 345 | if (false !== $str) { |
||
| 346 | $ret = mb_substr($string, $str + 1, mb_strlen($string)); |
||
| 347 | |||
| 348 | return $ret; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | return $string; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @public function getCamelCase |
||
| 357 | * @param $string |
||
| 358 | * @param $ucfirst |
||
| 359 | * @param $lcfirst |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getCamelCase($string, $ucfirst = false, $lcfirst = false) |
||
| 364 | { |
||
| 365 | $rightString = $this->getRightString($string); |
||
| 366 | $leftString = $this->getLeftString($string); |
||
| 367 | if ($ucfirst) { |
||
| 368 | return $this->getUcfirst($leftString) . $this->getUcfirst($rightString); |
||
| 369 | } |
||
| 370 | if ($lcfirst) { |
||
| 371 | return $this->getLcfirst($leftString) . $this->getUcfirst($rightString); |
||
| 372 | } |
||
| 373 | |||
| 374 | return $string; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @public function getUcfirst |
||
| 379 | * @param $string |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getUcfirst($string) |
||
| 384 | { |
||
| 385 | return ucfirst($string); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @public function getLcfirst |
||
| 390 | * |
||
| 391 | * @param $string |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getLcfirst($string) |
||
| 395 | { |
||
| 396 | return lcfirst($string); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @public function getStrToUpper |
||
| 401 | * |
||
| 402 | * @param $string |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getStrToUpper($string) |
||
| 406 | { |
||
| 407 | return mb_strtoupper($string); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @public function getStrToLower |
||
| 412 | * @param $string |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | public function getStrToLower($string) |
||
| 417 | { |
||
| 418 | return mb_strtolower($string); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @public function getInclude |
||
| 423 | * @param $filename |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getInclude($filename = 'header') |
||
| 427 | { |
||
| 428 | return "include __DIR__ . '/{$filename}.php';\n"; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @public function getIncludeOnce |
||
| 433 | * @param $filename |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function getIncludeOnce($filename = 'header') |
||
| 437 | { |
||
| 438 | return "include_once __DIR__ . '/{$filename}.php';\n"; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @private function getDashComment |
||
| 443 | * |
||
| 444 | * @param $comment |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getDashComment($comment) |
||
| 449 | { |
||
| 450 | return "// ------------------- {$comment} ------------------- //\n"; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @public function getSimpleString |
||
| 455 | * @param $string |
||
| 456 | * |
||
| 457 | * @param string $t |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getSimpleString($string, $t = '') |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @public function getHeaderFilesComments |
||
| 467 | * @param string $module |
||
| 468 | * @param string $fileName |
||
| 469 | * @param $noPhpFile |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getHeaderFilesComments($module, $fileName, $noPhpFile = null) |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @public function renderFile |
||
| 514 | * @param null |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function renderFile() |
||
| 551 | } |
||
| 552 | } |
||
| 553 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.