Completed
Push — master ( 99e371...869af4 )
by Brian
03:51
created

Publisher::getDestinationPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of Laravel Service Provider.
5
 *
6
 * (c) DraperStudio <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DraperStudio\ServiceProvider\Publisher;
13
14
use Illuminate\Filesystem\Filesystem;
15
16
abstract class Publisher
17
{
18
    /**
19
     * The filesystem instance.
20
     *
21
     * @var \Illuminate\Filesystem\Filesystem
22
     */
23
    protected $files;
24
25
    /**
26
     * The destination of the config files.
27
     *
28
     * @var string
29
     */
30
    protected $publishPath;
31
32
    /**
33
     * The path to the application's packages.
34
     *
35
     * @var string
36
     */
37
    protected $packagePath;
38
39
    /**
40
     * The destination paths for source files.
41
     *
42
     * @var string
43
     */
44
    protected $destinationPaths;
45
46
    /**
47
     * Create a new publisher instance.
48
     *
49
     * @param \Illuminate\Filesystem\Filesystem $files
50
     * @param string                            $publishPath
51
     */
52
    public function __construct(Filesystem $files, $publishPath)
53
    {
54
        $this->files = $files;
55
        $this->publishPath = $publishPath;
56
57
        $this->destinationPaths = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('migrations' => da...%s'), 'routes' => null) of type array<string,string|null...ring","routes":"null"}> is incompatible with the declared type string of property $destinationPaths.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
            'migrations' => database_path('/migrations/%s_%s'),
59
            'seeds' => database_path('/seeds/%s'),
60
            'config' => config_path('%s'),
61
            'views' => base_path('resources/views/vendor/%s'),
62
            'translations' => base_path('resources/lang/%s'),
63
            'assets' => public_path('vendor/%s'),
64
            'routes' => null,
65
        ];
66
    }
67
68
    /**
69
     * Publish files from a given path.
70
     *
71
     * @param string $package
72
     * @param string $source
0 ignored issues
show
Bug introduced by
There is no parameter named $source. 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...
73
     *
74
     * @return bool
75
     */
76
    public function getFileList($package)
77
    {
78
        return $this->getSource($package, $this->packagePath);
0 ignored issues
show
Unused Code introduced by
The call to Publisher::getSource() has too many arguments starting with $this->packagePath.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
79
    }
80
81
    /**
82
     * Set the default package path.
83
     *
84
     * @param string $packagePath
85
     */
86
    public function setPackagePath($packagePath)
87
    {
88
        $this->packagePath = $packagePath;
89
    }
90
91
    /**
92
     * Set the default package name.
93
     *
94
     * @param string $packageName
95
     */
96
    public function setPackageName($packageName)
97
    {
98
        $this->packageName = $packageName;
0 ignored issues
show
Bug introduced by
The property packageName 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...
99
    }
100
101
    /**
102
     * Get the source directory to publish.
103
     *
104
     * @param string $packagePath
105
     *
106
     * @return string
107
     *
108
     * @throws \InvalidArgumentException
109
     */
110
    abstract protected function getSource($packagePath);
111
112
    /**
113
     * @param $file
114
     *
115
     * @return string
116
     */
117
    protected function getFileName($file)
118
    {
119
        $file = basename($file);
120
121
        if (!ends_with($file, '.php')) {
122
            $file = $file.'.php';
123
        }
124
125
        return $file;
126
    }
127
128
    /**
129
     * Get the target destination path for the files.
130
     *
131
     * @param string $package
0 ignored issues
show
Bug introduced by
There is no parameter named $package. 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...
132
     *
133
     * @return string
134
     */
135
    protected function getDestinationPath($type, $args)
136
    {
137
        return vsprintf($this->destinationPaths[$type], $args);
138
    }
139
140
    /**
141
     * @param $type
142
     * @param $files
143
     *
144
     * @return array
145
     */
146
    protected function getSourceFiles($path)
147
    {
148
        $files = [];
149
        foreach (glob($path.'/*.php') as $file) {
150
            $files[] = $file;
151
        }
152
153
        return $files;
154
    }
155
156
    /**
157
     * @param $path
158
     *
159
     * @return mixed
160
     */
161
    protected function getClassFromFile($path)
162
    {
163
        $count = count($tokens = token_get_all(file_get_contents($path)));
164
165
        for ($i = 2; $i < $count; ++$i) {
166
            if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
167
                return $tokens[$i][1];
168
            }
169
        }
170
    }
171
}
172