Passed
Push — master ( 0a4c8d...e29dcc )
by Hamzah
04:13 queued 22s
created

Generator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 122
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 26 2
A getStub() 0 4 1
A generate() 0 18 1
A checkFolder() 0 8 2
1
<?php
2
3
namespace Shamaseen\Repository\Generator\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
8
/**
9
 * Class RepositoryGenerator
10
 * @package Shamaseen\Repository\Generator\Commands
11
 */
12
class Generator extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'make:repository
20
    {name : Class (singular) for example User}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create repository generator';
28
29
    /**
30
     * The repository name.
31
     *
32
     * @var string
33
     */
34
    protected $repoName;
35
    /**
36
     * The repository name space.
37
     *
38
     * @var string
39
     */
40
    protected $repoNamespace;
41
42
    /**
43
     * Create a new command instance.
44
     *
45
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
46
     */
47
    public function __construct()
48
    {
49
        parent::__construct();
50
    }
51
52
    /**
53
     * Execute the console command.
54
     *
55
     * @return void
56
     */
57
    public function handle()
58
    {
59
60
        $file = explode("/", (string)$this->argument('name'));
61
62
        $this->repoName=$file[count($file) - 1];
63
        unset($file[count($file) - 1]);
64
        $path = implode("/", $file);
65
66
        if(count($file) == 0){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
67
            $this->repoNamespace=$this->repoName;
68
        }else{
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; 0 found
Loading history...
69
            $name = $file[count($file) - 1];
70
            $this->repoNamespace=implode("\\", $file);
71
        }
72
73
74
        $this->generate($path, \Config::get('repository.controllers_path'), 'Controller');
75
        $this->generate($path, \Config::get('repository.models_path'), 'Entity');
76
        $this->generate($path, \Config::get('repository.requests_path'), 'Request');
77
        $this->generate($path, \Config::get('repository.interfaces_path'), 'Interface');
78
        $this->generate($path, \Config::get('repository.repositories_path'), 'Repository');
79
80
        File::append(\Config::get('repository.route_path').'web.php', "\n" . 'Route::resource(\'' . str_plural($name) . "', '{$name}Controller');");
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
82
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
83
84
    /**
85
     * Get stub content to generate needed files
86
     *
87
     * @param string $type  determine which stub should choose to get content
88
     * @return false|string
89
     */
90
    protected function getStub($type)
91
    {
92
        return file_get_contents(\Config::get('repository.resources_path')."/stubs/$type.stub");
93
    }
94
95
    /**
96
     * @param string $name Class name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
     * @param string $path Class path
98
     * @param string $folder default path to generate in
99
     * @param string $type define which kind of files should generate
100
     */
101
    protected function generate($path, $folder, $type)
102
    {
103
104
        $template = str_replace(
105
            [
106
                '{{modelName}}',
107
                "{{modelNamePlural}}"
108
            ],
109
            [
110
                $this->repoName,
111
                $this->repoNamespace
112
            ],
113
            $this->getStub($type)
114
        );
115
        $path = $this->checkFolder(\Config::get('repository.app_path').'/'.$folder.$path."/");
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $path. This often makes code more readable.
Loading history...
116
        file_put_contents($path . "{$this->repoName}{$type}.php", $template);
117
118
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
119
120
    /**
121
     * Check if folder exist
122
     * @param string $path class path
123
     * @return string
124
     */
125
    public function checkFolder($path)
126
    {
127
128
        if (!file_exists($path)) {
129
            mkdir($path, 0777, true);
130
        }
131
        return $path;
132
    }
133
}
134