MakeFormCommand::addFormMethodToController()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
4
use Symfony\Component\Console\Input\InputOption;
5
6
class MakeFormCommand extends AbstractMakeCommand
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $name = 'make:form';
12
13
    /**
14
     * @var string
15
     */
16
    protected $description = 'Create a new Form class and optional template';
17
18
    protected function writeFile($target, $class)
19
    {
20
        parent::writeFile($target, $class);
21
22
        if ($controller = $this->option('controller')) {
23
            $filePath = $this->findControllerFilePath($controller);
24
            if ($filePath !== false) {
25
                $this->addFormMethodToController($controller, $class);
0 ignored issues
show
Bug introduced by
It seems like $controller defined by $this->option('controller') on line 22 can also be of type array; however, MakeFormCommand::addFormMethodToController() 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...
26
                $content = file_get_contents($filePath);
27
28
                $content = $this->addFormMethodToController($content, $class);
29
                $this->info($class.' method added to ', $controller);
0 ignored issues
show
Bug introduced by
It seems like $controller defined by $this->option('controller') on line 22 can also be of type array; however, SilverstripeCommand::info() does only seem to accept null|integer|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...
30
                $content = $this->addAllowedActionsToController($content, $class);
31
                $this->warn('Adding allowed_actions not supported yet');
32
33
                file_put_contents($filePath, $content);
34
            }
35
        }
36
    }
37
38
    /**
39
     * @param string $content
40
     * @param string $formClass
41
     *
42
     * @return string
43
     */
44
    protected function addFormMethodToController($content, $formClass)
45
    {
46
        if (!Str::contains($content, "function $formClass(")) {
47
            $methodStub = $this->getFormMethodStub($formClass);
48
            $replacement = $methodStub."\n}";
49
            if ($methodStub) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methodStub of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
50
                $content = Str::replaceLast('}', $replacement, $content);
51
            }
52
        }
53
54
        return $content;
55
    }
56
57
    /**
58
     * @param string $content
59
     * @param string $formClass
60
     *
61
     * @return string
62
     */
63
    protected function addAllowedActionsToController($content, $formClass)
0 ignored issues
show
Unused Code introduced by
The parameter $formClass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        $allowedActions = 'private static $allowed_actions';
66
67
        if (!Str::contains($content, $allowedActions)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
68
        }
69
70
        return $content;
71
    }
72
73
    /**
74
     * @param $controller
75
     *
76
     * @return bool|string
77
     */
78
    protected function findControllerFilePath($controller)
79
    {
80
        $controller = strtolower($controller);
81
        $classes = SS_ClassLoader::instance()->getManifest()->getClasses();
82
        $filePath = isset($classes[$controller]) ? $classes[$controller] : '';
83
84
        if (!is_file($filePath)) {
85
            $this->error("$filePath does not exist");
86
87
            return false;
88
        }
89
90
        return $filePath;
91
    }
92
93
    protected function getFormMethodStub($formClass)
94
    {
95
        $file = $this->getStubFilePath('ControllerFormMethods');
96
97
        if (is_file($file)) {
98
            return str_replace('DummyClass', $formClass, file_get_contents($file));
99
        }
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    protected function getOptions()
106
    {
107
        $options = parent::getOptions();
108
        $options[] = ['template', 't', InputOption::VALUE_NONE, 'Create a custom template for this Form'];
109
        $options[] = ['controller', 'C', InputOption::VALUE_REQUIRED, 'Add FormMethod and allowed actions to the given Controller'];
110
111
        return $options;
112
    }
113
}
114