Test Setup Failed
Push — master ( d6fde7...1e848b )
by Php Easy Api
04:20
created

Stubber::get()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 28
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 51
rs 9.472

How to fix   Long Method   

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 Migratio\Resource\StubManager;
4
5
use Migratio\Schema;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
8
9
class Stubber
10
{
11
    /**
12
     * @var $config
0 ignored issues
show
Documentation Bug introduced by
The doc comment $config at position 0 could not be parsed: Unknown type name '$config' at position 0 in $config.
Loading history...
13
     */
14
    protected $config;
15
16
    /**
17
     * @var $schema Schema
0 ignored issues
show
Documentation Bug introduced by
The doc comment $schema at position 0 could not be parsed: Unknown type name '$schema' at position 0 in $schema.
Loading history...
18
     */
19
    protected $schema;
20
21
    /**
22
     * @var $stubber
0 ignored issues
show
Documentation Bug introduced by
The doc comment $stubber at position 0 could not be parsed: Unknown type name '$stubber' at position 0 in $stubber.
Loading history...
23
     */
24
    protected $stubber;
25
26
    /**
27
     * @var $file
0 ignored issues
show
Documentation Bug introduced by
The doc comment $file at position 0 could not be parsed: Unknown type name '$file' at position 0 in $file.
Loading history...
28
     */
29
    protected $file;
30
31
    public function __construct($schema)
32
    {
33
        $this->file     = new Filesystem();
34
        $this->stubber  = realpath(__DIR__.'/../../Stub');
35
        $this->schema   = $schema;
36
        $this->config   = $this->schema->getConfig();
37
    }
38
39
    /**
40
     * @param $params
41
     * @return array
42
     */
43
    private function getPaths($params)
44
    {
45
        if(!isset($params[3])){
46
            return [$this->config['paths'][0]];
47
        }
48
    }
49
50
    /**
51
     * @param $executionPath
52
     * @param $path
53
     * @param $params
54
     * @return bool
55
     */
56
    private function fopenprocess($executionPath,$path,$params)
57
    {
58
        $dt = fopen($executionPath, "r");
59
        $content = fread($dt, filesize($executionPath));
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $content = fread(/** @scrutinizer ignore-type */ $dt, filesize($executionPath));
Loading history...
60
        fclose($dt);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        fclose(/** @scrutinizer ignore-type */ $dt);
Loading history...
61
62
        foreach ($params as $key=>$value){
63
64
            $content=str_replace("__".$key."__",$value,$content);
65
        }
66
67
68
        $dt = fopen($path, "w");
69
        fwrite($dt, $content);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

69
        fwrite(/** @scrutinizer ignore-type */ $dt, $content);
Loading history...
70
        fclose($dt);
71
72
        return true;
73
74
75
    }
76
77
    /**
78
     * @param $params
79
     */
80
    public function get($params)
81
    {
82
        list($table,$name,$type) = $params;
83
84
        $results = [];
85
86
        $paths = $this->getPaths($params);
87
88
        foreach ($paths as $pathKey=>$path){
89
90
            $tableDirectory = $path.'/'.ucfirst($table);
91
92
93
            if(!file_exists($tableDirectory)){
94
                $results['directory'][$pathKey]= $this->fileProcess($tableDirectory,'makeDirectory');
95
            }
96
            else{
97
                $results['directory'][$pathKey]= $this->getResult(false,
98
                    'Already exist the specified directory');
99
            }
100
101
            $results['directory'][$pathKey]['table']= $table;
102
            $results['directory'][$pathKey]['directory']= $table;
103
            $results['directory'][$pathKey]['type']= $type;
104
105
            $fileName = ucfirst($name);
106
107
            $fileNameWithTime = time().'_'.$fileName;
108
109
            $filePath = $tableDirectory.'/'.$fileNameWithTime.'.php';
110
111
            if(!file_exists($filePath)){
112
                $results['file'][$pathKey]=$this->fileProcess($filePath,'makeFile');
113
            }
114
            else{
115
                $results['file'][$pathKey]= $this->getResult(false,
116
                    'Already exist the specified file');
117
            }
118
119
            $results['file'][$pathKey]['table']=$table;
120
            $results['file'][$pathKey]['file']=$fileName;
121
            $results['file'][$pathKey]['type']=$type;
122
123
            $stubber = $this->stubber.'/'.$type.'.stub';
124
125
            $this->fopenprocess($stubber,$filePath,['className'=>$fileName]);
126
127
            $this->file->chmod($tableDirectory,0777,000,true);
128
        }
129
130
        return $results;
131
    }
132
133
    /**
134
     * @param $path
135
     * @param $process
136
     * @return array
137
     */
138
    private function fileProcess($path,$process)
139
    {
140
        try {
141
            $this->{$process}($path);
142
            return $this->getResult(true,null);
143
        } catch (IOExceptionInterface $exception) {
144
            return $this->getResult(false,
145
                "An error occurred while creating your directory at ".$exception->getPath()."");
146
        }
147
    }
148
149
    /**
150
     * @param $path
151
     * @param string $mode
152
     */
153
    private function  makeDirectory($path,$mode='0777')
154
    {
155
        return $this->file->mkdir($path,$mode);
0 ignored issues
show
Bug introduced by
$mode of type string is incompatible with the type integer expected by parameter $mode of Symfony\Component\Filesystem\Filesystem::mkdir(). ( Ignorable by Annotation )

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

155
        return $this->file->mkdir($path,/** @scrutinizer ignore-type */ $mode);
Loading history...
Bug introduced by
Are you sure the usage of $this->file->mkdir($path, $mode) targeting Symfony\Component\Filesystem\Filesystem::mkdir() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
156
    }
157
158
    /**
159
     * @param $path
160
     */
161
    private function makeFile($path)
162
    {
163
        return $this->file->touch($path);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->file->touch($path) targeting Symfony\Component\Filesystem\Filesystem::touch() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
164
    }
165
166
    /**
167
     * @param $success
168
     * @param $message
169
     * @return array
170
     */
171
    private function getResult($success,$message)
172
    {
173
        return [
174
            'success'=>$success,
175
            'message'=>$message
176
        ];
177
    }
178
}
179
180