GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MigrationGenerator::getStub()   C
last analyzed

Complexity

Conditions 10
Paths 32

Size

Total Lines 51
Code Lines 38

Duplication

Lines 18
Ratio 35.29 %

Importance

Changes 0
Metric Value
cc 10
eloc 38
nc 32
nop 0
dl 18
loc 51
rs 6
c 0
b 0
f 0

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 Milkmeowo\Framework\Repository\Generators;
4
5
use Milkmeowo\Framework\Repository\Generators\Migrations\NameParser;
6
use Milkmeowo\Framework\Repository\Generators\Migrations\SchemaParser;
7
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
8
9
class MigrationGenerator extends Generator
10
{
11
    /**
12
     * Get stub name.
13
     *
14
     * @var string
15
     */
16
    protected $stub = 'migration/plain';
17
18
    /**
19
     * Get base path of destination file.
20
     *
21
     * @return string
22
     */
23
    public function getBasePath()
24
    {
25
        return base_path().'/database/migrations/';
26
    }
27
28
    /**
29
     * Get destination path for generated file.
30
     *
31
     * @return string
32
     */
33
    public function getPath()
34
    {
35
        return $this->getBasePath().$this->getFileName().'.php';
36
    }
37
38
    /**
39
     * Get generator path config node.
40
     *
41
     * @return string
42
     */
43
    public function getPathConfigNode()
44
    {
45
        return '';
46
    }
47
48
    /**
49
     * Get root namespace.
50
     *
51
     * @return string
52
     */
53
    public function getRootNamespace()
54
    {
55
        return '';
56
    }
57
58
    /**
59
     * Get migration name.
60
     *
61
     * @return string
62
     */
63
    public function getMigrationName()
64
    {
65
        return strtolower($this->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Milkmeowo\Framewo...ors\MigrationGenerator>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
    }
67
68
    /**
69
     * Get file name.
70
     *
71
     * @return string
72
     */
73
    public function getFileName()
74
    {
75
        return date('Y_m_d_His_').$this->getMigrationName();
76
    }
77
78
    /**
79
     * Get schema parser.
80
     *
81
     * @return SchemaParser
82
     */
83
    public function getSchemaParser()
84
    {
85
        return new SchemaParser($this->fields);
0 ignored issues
show
Documentation introduced by
The property fields does not exist on object<Milkmeowo\Framewo...ors\MigrationGenerator>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
    }
87
88
    /**
89
     * Get name parser.
90
     *
91
     * @return NameParser
92
     */
93
    public function getNameParser()
94
    {
95
        return new NameParser($this->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Milkmeowo\Framewo...ors\MigrationGenerator>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
    }
97
98
    /**
99
     * Get stub templates.
100
     *
101
     * @return Stub
102
     */
103
    public function getStub()
104
    {
105
        $parser = $this->getNameParser();
106
107
        $action = $parser->getAction();
108
        switch ($action) {
109
            case 'add':
110
            case 'append':
111
            case 'update':
112 View Code Duplication
            case 'insert':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
                $file = 'change';
114
                $replacements = [
115
                    'class'       => $this->getClass(),
116
                    'table'       => $parser->getTable(),
117
                    'fields_up'   => $this->getSchemaParser()->up(),
118
                    'fields_down' => $this->getSchemaParser()->down(),
119
                ];
120
                break;
121
122
            case 'delete':
123
            case 'remove':
124 View Code Duplication
            case 'alter':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
                $file = 'change';
126
                $replacements = [
127
                    'class'       => $this->getClass(),
128
                    'table'       => $parser->getTable(),
129
                    'fields_down' => $this->getSchemaParser()->up(),
130
                    'fields_up'   => $this->getSchemaParser()->down(),
131
                ];
132
                break;
133
            default:
134
                $file = 'create';
135
                $replacements = [
136
                    'class'  => $this->getClass(),
137
                    'table'  => $parser->getTable(),
138
                    'fields' => $this->getSchemaParser()->up(),
139
                ];
140
                break;
141
        }
142
        $path = config('repository.generator.stubsOverridePath', __DIR__);
143
144
        if (! file_exists($path."/Stubs/migration/{$file}.stub")) {
145
            $path = __DIR__;
146
        }
147
148
        if (! file_exists($path."/Stubs/migration/{$file}.stub")) {
149
            throw new FileNotFoundException($path."/Stubs/migration/{$file}.stub");
150
        }
151
152
        return Stub::create($path."/Stubs/migration/{$file}.stub", $replacements);
153
    }
154
}
155