Passed
Push — master ( 25273e...fcf8a0 )
by Mihail
04:34
created

ArchBuilder::createObject()   D

Complexity

Conditions 15
Paths 205

Size

Total Lines 79
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 79
rs 4.6621
cc 15
eloc 52
nc 205
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Apps\Model\Console;
4
5
use Ffcms\Core\Helper\FileSystem\Directory;
6
use Ffcms\Core\Helper\FileSystem\File;
7
use Ffcms\Core\Helper\Type\Str;
8
9
class ArchBuilder
10
{
11
    public $message;
12
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;
0 ignored issues
show
Unused Code introduced by
$objectDirPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $objectNamespace = null;
0 ignored issues
show
Unused Code introduced by
$objectNamespace is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
        $objectName = null;
0 ignored issues
show
Unused Code introduced by
$objectName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $objectTemplate can also be of type boolean; however, Ffcms\Core\Helper\Type\Str::replace() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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
}