Passed
Push — master ( 32dee0...5d2de3 )
by Andrea Marco
13:08 queued 11s
created

AbstractCreatorTask::getPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Cerbero\ConsoleTasker\Tasks;
4
5
use Cerbero\ConsoleTasker\ManipulatedFile;
6
use Illuminate\Container\Container;
7
use RuntimeException;
8
9
/**
10
 * The abstract creator task.
11
 *
12
 */
13
abstract class AbstractCreatorTask extends AbstractFilesManipulatorTask
14
{
15
    /**
16
     * Retrieve the path of the stub
17
     *
18
     * @return string
19
     */
20
    abstract protected function getStubPath(): string;
21
22
    /**
23
     * Run the task
24
     *
25
     * @return mixed
26
     */
27
    public function run()
28
    {
29
        $file = $this->file($this->getPath());
30
31
        if ($reason = $this->needsManualUpdateTo()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $reason is correct as $this->needsManualUpdateTo() targeting Cerbero\ConsoleTasker\Ta...::needsManualUpdateTo() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
32
            $file->needsManualUpdateTo($reason);
0 ignored issues
show
Bug introduced by
$reason of type void is incompatible with the type string expected by parameter $reason of Cerbero\ConsoleTasker\Ma...::needsManualUpdateTo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            $file->needsManualUpdateTo(/** @scrutinizer ignore-type */ $reason);
Loading history...
33
        }
34
35
        if ($this->canCreateFile($file)) {
36
            return $this->createFile($file);
37
        }
38
39
        $this->setError($this->getCreationError($file));
40
41
        return false;
42
    }
43
44
    /**
45
     * Retrieve the path of the file to create
46
     *
47
     * @return string
48
     */
49
    protected function getPath(): string
50
    {
51
        if (is_null($name = $this->getFullyQualifiedName())) {
0 ignored issues
show
introduced by
The condition is_null($name = $this->getFullyQualifiedName()) is always true.
Loading history...
Bug introduced by
Are you sure the assignment to $name is correct as $this->getFullyQualifiedName() targeting Cerbero\ConsoleTasker\Ta...getFullyQualifiedName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
            throw new RuntimeException('Please provide a path or a fully qualified name for the file to create');
53
        }
54
55
        if (strpos($name, $namespace = Container::getInstance()->make('app')->getNamespace()) === 0) {
56
            $name = substr_replace($name, 'app/', 0, strlen($namespace));
57
        }
58
59
        return Container::getInstance()->make('app')->basePath(str_replace('\\', '/', $name)) . '.php';
60
    }
61
62
    /**
63
     * Retrieve the fully qualified name of the file to create
64
     *
65
     * @return string|null
66
     */
67
    protected function getFullyQualifiedName(): ?string
68
    {
69
        return null;
70
    }
71
72
    /**
73
     * Retrieve the reason why the file needs to be updated manually
74
     *
75
     * @return string|null
76
     */
77
    public function needsManualUpdateTo(): ?string
78
    {
79
        return null;
80
    }
81
82
    /**
83
     * Determine whether the file can be created
84
     *
85
     * @param ManipulatedFile $file
86
     * @return bool
87
     */
88
    protected function canCreateFile(ManipulatedFile $file): bool
89
    {
90
        if ($this->hasOption('force') && $this->option('force')) {
91
            return true;
92
        }
93
94
        return !file_exists($file->getPath());
95
    }
96
97
    /**
98
     * Retrieve the reason why the file could not be created
99
     *
100
     * @param ManipulatedFile $file
101
     * @return string
102
     */
103
    protected function getCreationError(ManipulatedFile $file): string
104
    {
105
        return 'The file ' . basename($file->getPath()) . ' already exists';
106
    }
107
108
    /**
109
     * Create the file
110
     *
111
     * @param ManipulatedFile $file
112
     * @return bool
113
     */
114
    protected function createFile(ManipulatedFile $file): bool
115
    {
116
        $path = $file->getPath();
117
118
        if (!is_dir(dirname($path))) {
119
            @mkdir(dirname($path), 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

119
            /** @scrutinizer ignore-unhandled */ @mkdir(dirname($path), 0777, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
120
        }
121
122
        $replacements = array_merge($this->getDefaultReplacements(), $this->getReplacements());
123
        $stubContent = file_get_contents($this->getStubPath());
124
        $content = str_replace(array_keys($replacements), array_values($replacements), $stubContent);
125
126
        return file_put_contents($path, $content) !== false;
127
    }
128
129
    /**
130
     * Retrieve the default replacements to apply on the stub
131
     *
132
     * @return array
133
     */
134
    protected function getDefaultReplacements(): array
135
    {
136
        $qualified = $this->getFullyQualifiedName();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $qualified is correct as $this->getFullyQualifiedName() targeting Cerbero\ConsoleTasker\Ta...getFullyQualifiedName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
        $class = class_basename($qualified);
138
        $namespace = substr($qualified, 0, strrpos($qualified, $class) - 1);
0 ignored issues
show
Bug introduced by
$qualified of type null is incompatible with the type string expected by parameter $haystack of strrpos(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
        $namespace = substr($qualified, 0, strrpos(/** @scrutinizer ignore-type */ $qualified, $class) - 1);
Loading history...
Bug introduced by
$qualified of type null is incompatible with the type string expected by parameter $string of substr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
        $namespace = substr(/** @scrutinizer ignore-type */ $qualified, 0, strrpos($qualified, $class) - 1);
Loading history...
139
140
        return [
141
            'DummyClass' => $class,
142
            '{{ class }}' => $class,
143
            'DummyNamespace' => $namespace,
144
            '{{ namespace }}' => $namespace,
145
        ];
146
    }
147
148
    /**
149
     * Retrieve the replacements to apply on the stub
150
     *
151
     * @return array
152
     */
153
    protected function getReplacements(): array
154
    {
155
        return [];
156
    }
157
}
158