Passed
Push — master ( 1f7f34...948e88 )
by Thomas
04:12 queued 02:03
created

CanGenerateFile::loadStubFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Traits;
4
5
trait CanGenerateFile
6
{
7
    protected $fileContent;
8
9
    public function call()
10
    {
11
        $this->generateFile();
12
        $this->addGeneratedFileToIdeStack();
0 ignored issues
show
Bug introduced by
It seems like addGeneratedFileToIdeStack() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
13
    }
14
15
    /**
16
     * Generate the file and save it according to specified naming.
17
     *
18
     * @return void
19
     */
20
    protected function generateFile(): void
21
    {
22
        if (method_exists($this->naming, 'getStub')) {
23
            $this->loadStubFile($this->naming->getStub());
0 ignored issues
show
Bug introduced by
The property naming does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        }
25
26
        $this->buildFileContent();
0 ignored issues
show
Bug introduced by
It seems like buildFileContent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
28
        if (!$this->filesystem->isDirectory($this->naming->getPath())) {
29
            $this->filesystem->makeDirectory($this->naming->getPath());
0 ignored issues
show
Bug introduced by
The property filesystem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
        }
31
32
        $this->filesystem->put($this->naming->getFile(), $this->fileContent);
33
    }
34
35
    protected function loadStubFile(string $stubFile): void
36
    {
37
        try {
38
            $this->fileContent = $this->filesystem->get($stubFile);
39
        } catch (FileNotFoundException $exception) {
0 ignored issues
show
Bug introduced by
The class Webfactor\Laravel\Genera...s\FileNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
40
            $this->command->error('Could not find stub file: ' . $stubFile);
0 ignored issues
show
Bug introduced by
The property command does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
        }
42
    }
43
44
    /**
45
     * Replace the class namespace in stub file.
46
     *
47
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49
    protected function replaceClassNamespace(): void
50
    {
51
        $this->fileContent = str_replace('__class_namespace__', $this->naming->getNamespace(), $this->fileContent);
52
    }
53
54
    /**
55
     * Replace the class name in stub file.
56
     *
57
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
     */
59
    protected function replaceClassName(): void
60
    {
61
        $this->fileContent = str_replace('__class_name__', $this->naming->getClassName(), $this->fileContent);
62
    }
63
64
    /**
65
     * Replace the table name in stub file.
66
     *
67
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    protected function replaceTableName(): void
70
    {
71
        $this->fileContent = str_replace('__table_name__', $this->command->naming['migration']->getTableName(), $this->fileContent);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
72
    }
73
}
74