Passed
Push — 1.2 ( ac7648...d64a15 )
by Quentin
05:40
created

fix_directory_separator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
use Illuminate\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Filesystem. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use Illuminate\Support\Arr;
5
use Illuminate\Support\Facades\Event;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Event. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use \Illuminate\Support\Str;
7
8
if (!function_exists('dumpUsableSqlQuery')) {
9
    function dumpUsableSqlQuery($query)
10
    {
11
        dd(vsprintf(str_replace('?', '%s', $query->toSql()), array_map(function ($binding) {
12
            return is_numeric($binding) ? $binding : "'{$binding}'";
13
        }, $query->getBindings())));
14
    }
15
}
16
17
if (!function_exists('classUsesDeep')) {
18
    /**
19
     * @param mixed $class
20
     * @param bool $autoload
21
     * @return array
22
     */
23
    function classUsesDeep($class, $autoload = true)
24
    {
25
        $traits = [];
26
27
        // Get traits of all parent classes
28
        do {
29
            $traits = array_merge(class_uses($class, $autoload), $traits);
30
        } while ($class = get_parent_class($class));
31
32
        // Get traits of all parent traits
33
        $traitsToSearch = $traits;
34
        while (!empty($traitsToSearch)) {
35
            $newTraits = class_uses(array_pop($traitsToSearch), $autoload);
36
            $traits = array_merge($newTraits, $traits);
37
            $traitsToSearch = array_merge($newTraits, $traitsToSearch);
38
        }
39
40
        foreach ($traits as $trait => $same) {
41
            $traits = array_merge(class_uses($trait, $autoload), $traits);
42
        }
43
44
        return array_unique($traits);
45
    }
46
}
47
48
if (!function_exists('classHasTrait')) {
49
    /**
50
     * @param mixed $class
51
     * @param string $trait
52
     * @return bool
53
     */
54
    function classHasTrait($class, $trait)
55
    {
56
        $traits = classUsesDeep($class);
57
58
        if (in_array($trait, array_keys($traits))) {
59
            return true;
60
        }
61
62
        return false;
63
    }
64
}
65
66
if (!function_exists('getFormFieldsValue')) {
67
    /**
68
     * @param array $formFields
69
     * @param string $name
70
     * @return mixed
71
     */
72
    function getFormFieldsValue($formFields, $name)
73
    {
74
        return Arr::get($formFields, str_replace(']', '', str_replace('[', '.', $name)), '');
75
    }
76
}
77
78
if (!function_exists('fireCmsEvent')) {
79
    /**
80
     * @param string $eventName
81
     * @param array $input
82
     * @return void
83
     */
84
    function fireCmsEvent($eventName, $input = [])
85
    {
86
        $method = method_exists(\Illuminate\Events\Dispatcher::class, 'dispatch') ? 'dispatch' : 'fire';
87
        Event::$method($eventName, [$eventName, $input]);
88
    }
89
}
90
91
if (!function_exists('twill_path')) {
92
    /**
93
     * @param string $path
94
     * @return string
95
     */
96
    function twill_path($path = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
    {
98
        // Is it a full application path?
99
        if (Str::startsWith($path, base_path())) {
100
            return $path;
101
        }
102
103
        // Split to separate root namespace
104
        preg_match('/(\w*)\W?(.*)/', config('twill.namespace'), $namespaceParts);
105
106
        $twillBase = app_path(
107
            fix_directory_separator(
108
                $namespaceParts[1] == 'App' ? $namespaceParts[2] : $namespaceParts[0]
109
            )
110
        ) . '/';
111
112
        // Remove base path from path
113
        if (Str::startsWith($path, $twillBase)) {
114
            $path = Str::after($path, $twillBase);
115
        }
116
117
        // Namespace App is unchanged in config?
118
        if ($namespaceParts[0] === 'App') {
119
            return app_path($path);
120
        }
121
122
        // If it it still starts with App, use the left part, otherwise use the whole namespace
123
        // This can be a problem for those using a completely different app path for the application
124
        $left = ($namespaceParts[1] === 'App' ? $namespaceParts[2] : $namespaceParts[0]);
125
126
        // Join, fix slashes for the current operating system, and return path
127
        return app_path(fix_directory_separator(
128
            $left . (filled($path) ? '\\' . $path : '')
129
        ));
130
    }
131
}
132
133
if (!function_exists('make_twill_directory')) {
134
    /**
135
     * @param string $path
136
     * @param bool $recursive
137
     * @param \Illuminate\Filesystem\Filesystem|null $fs
138
     */
139
    function make_twill_directory($path, $recursive = true, $fs = null)
140
    {
141
        $fs = filled($fs)
142
            ? $fs
143
            : app(Filesystem::class);
144
145
        $path = twill_path($path);
146
147
        if (!$fs->isDirectory($path)) {
0 ignored issues
show
Bug introduced by
The method isDirectory() does not exist on null. ( Ignorable by Annotation )

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

147
        if (!$fs->/** @scrutinizer ignore-call */ isDirectory($path)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            $fs->makeDirectory($path, 0755, $recursive);
149
        }
150
    }
151
}
152
153
if (!function_exists('twill_put_stub')) {
154
    /**
155
     * @param string $path
156
     * @param bool $recursive
157
     * @param \Illuminate\Filesystem\Filesystem|null $fs
158
     */
159
    function twill_put_stub($path, $stub, $fs = null)
160
    {
161
        $fs = filled($fs)
162
            ? $fs
163
            : app(Filesystem::class);
164
165
        $stub = str_replace(
166
            'namespace App\\',
167
            sprintf('namespace %s\\', config('twill.namespace')),
168
            $stub
169
        );
170
171
        if ($fs->missing($path)) {
172
            $fs->put($path, $stub);
173
        }
174
    }
175
}
176
177
if (!function_exists('fix_directory_separator')) {
178
    /**
179
     * @param string $path
180
     * @param bool $recursive
181
     * @param int $mode
182
     */
183
    function fix_directory_separator($path)
184
    {
185
        return str_replace(
186
            '\\',
187
            DIRECTORY_SEPARATOR,
188
            $path
189
        );
190
    }
191
}
192
193