| Conditions | 15 |
| Paths | 205 |
| Total Lines | 79 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 13 | public function createObject($name, $type) |
||
| 14 | { |
||
| 15 | $singleName = false; |
||
| 16 | if (!Str::contains('/', $name)) { |
||
| 17 | if ($type === 'ActiveRecord') { |
||
| 18 | $singleName = true; |
||
| 19 | } else { |
||
| 20 | $this->message = 'Command dosn\'t contains valid name. Example: Front/SomeForm, Admin/SomePkg/SomeInput'; |
||
| 21 | return false; |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | $objectDirPath = null; |
||
|
|
|||
| 26 | $objectNamespace = null; |
||
| 27 | $objectName = null; |
||
| 28 | |||
| 29 | $objectTemplate = null; |
||
| 30 | |||
| 31 | if ($singleName) { |
||
| 32 | $objectDirPath = root . '/Apps/' . $type . '/'; |
||
| 33 | $objectNamespace = 'Apps\\' . $type; |
||
| 34 | $objectName = ucfirst($name); |
||
| 35 | } else { |
||
| 36 | $split = explode('/', $name); |
||
| 37 | $workground = ucfirst(strtolower(array_shift($split))); |
||
| 38 | $objectName = ucfirst(array_pop($split)); |
||
| 39 | |||
| 40 | $subName = false; |
||
| 41 | if (count($split) > 0) { // some sub-namespace / folder path |
||
| 42 | foreach ($split as $part) { |
||
| 43 | if (Str::length($part) > 0) { |
||
| 44 | $subName[] = ucfirst(strtolower($part)); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($type === 'Widget') { |
||
| 50 | $objectDirPath = root . '/Widgets/' . $workground; |
||
| 51 | $objectNamespace = 'Widgets\\' . $workground; |
||
| 52 | } else { |
||
| 53 | $objectDirPath = root . '/Apps/' . $type . '/' . $workground; |
||
| 54 | $objectNamespace = 'Apps\\' . $type . '\\' . $workground; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (false !== $subName) { |
||
| 58 | $objectDirPath .= '/' . implode('/', $subName); |
||
| 59 | $objectNamespace .= '\\' . implode('\\', $subName); |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | // try to find workground-based controller |
||
| 64 | if (File::exist('/Private/Carcase/' . $workground . '/' . $type . '.tphp')) { |
||
| 65 | $objectTemplate = File::read('/Private/Carcase/' . $workground . '/' . $type . '.tphp'); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!Directory::exist($objectDirPath) && !Directory::create($objectDirPath)) { |
||
| 70 | $this->message = 'Directory could not be created: ' . $objectDirPath; |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($objectTemplate === null) { |
||
| 75 | $objectTemplate = File::read('/Private/Carcase/' . $type . '.tphp'); |
||
| 76 | if (false === $objectTemplate) { |
||
| 77 | $this->message = 'Php template file is not founded: /Private/Carcase/' . $type . '.tphp'; |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $objectContent = Str::replace(['%namespace%', '%name%'], [$objectNamespace, $objectName], $objectTemplate); |
||
| 83 | $objectFullPath = $objectDirPath . '/' . $objectName . '.php'; |
||
| 84 | if (File::exist($objectFullPath)) { |
||
| 85 | $this->message = $type . ' is always exist: ' . $objectFullPath; |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | File::write($objectFullPath, $objectContent); |
||
| 89 | $this->message = $type . ' template was created: [' . $objectName . '] in path: ' . Str::replace(root, '', $objectDirPath); |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.