Passed
Push — master ( 136870...398928 )
by Quentin
53:06 queued 43:01
created

generate_list_of_available_blocks()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
nc 1
nop 2
dl 0
loc 30
ccs 0
cts 0
cp 0
crap 72
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
use A17\Twill\Services\Blocks\BlockCollection;
4
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...
5
use Illuminate\Support\Arr;
6
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...
7
use \Illuminate\Support\Str;
8
9
if (!function_exists('dumpUsableSqlQuery')) {
10
    function dumpUsableSqlQuery($query)
11
    {
12
        dd(vsprintf(str_replace('?', '%s', $query->toSql()), array_map(function ($binding) {
13
            return is_numeric($binding) ? $binding : "'{$binding}'";
14
        }, $query->getBindings())));
15
    }
16
}
17
18
if (!function_exists('classUsesDeep')) {
19
    /**
20
     * @param mixed $class
21
     * @param bool $autoload
22
     * @return array
23
     */
24
    function classUsesDeep($class, $autoload = true)
25 29
    {
26
        $traits = [];
27
28
        // Get traits of all parent classes
29 29
        do {
30 29
            $traits = array_merge(class_uses($class, $autoload), $traits);
31
        } while ($class = get_parent_class($class));
32
33 29
        // Get traits of all parent traits
34 29
        $traitsToSearch = $traits;
35 29
        while (!empty($traitsToSearch)) {
36 29
            $newTraits = class_uses(array_pop($traitsToSearch), $autoload);
37 29
            $traits = array_merge($newTraits, $traits);
38
            $traitsToSearch = array_merge($newTraits, $traitsToSearch);
39
        }
40 29
41 29
        foreach ($traits as $trait => $same) {
42
            $traits = array_merge(class_uses($trait, $autoload), $traits);
43
        }
44 29
45
        return array_unique($traits);
46
    }
47
}
48
49
if (!function_exists('classHasTrait')) {
50
    /**
51
     * @param mixed $class
52
     * @param string $trait
53
     * @return bool
54
     */
55
    function classHasTrait($class, $trait)
56 29
    {
57
        $traits = classUsesDeep($class);
58 29
59 26
        if (in_array($trait, array_keys($traits))) {
60
            return true;
61
        }
62 3
63
        return false;
64
    }
65
}
66
67
if (!function_exists('getFormFieldsValue')) {
68
    /**
69
     * @param array $formFields
70
     * @param string $name
71
     * @return mixed
72
     */
73
    function getFormFieldsValue($formFields, $name)
74 2
    {
75
        return Arr::get($formFields, str_replace(']', '', str_replace('[', '.', $name)), '');
76
    }
77
}
78
79
if (!function_exists('fireCmsEvent')) {
80
    /**
81
     * @param string $eventName
82
     * @param array $input
83
     * @return void
84
     */
85
    function fireCmsEvent($eventName, $input = [])
86 21
    {
87 21
        $method = method_exists(\Illuminate\Events\Dispatcher::class, 'dispatch') ? 'dispatch' : 'fire';
88 21
        Event::$method($eventName, [$eventName, $input]);
89
    }
90
}
91
92
if (!function_exists('twill_path')) {
93
    /**
94
     * @param string $path
95
     * @return string
96
     */
97
    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...
98
    {
99 55
        // Is it a full application path?
100 1
        if (Str::startsWith($path, base_path())) {
101
            return $path;
102
        }
103
104 55
        // Split to separate root namespace
105
        preg_match('/(\w*)\W?(.*)/', config('twill.namespace'), $namespaceParts);
106 55
107 55
        $twillBase = app_path(
108 55
            fix_directory_separator(
109
                $namespaceParts[1] == 'App' ? $namespaceParts[2] : $namespaceParts[0]
110 55
            )
111
        ) . '/';
112
113 55
        // Remove base path from path
114
        if (Str::startsWith($path, $twillBase)) {
115
            $path = Str::after($path, $twillBase);
116
        }
117
118 55
        // Namespace App is unchanged in config?
119 55
        if ($namespaceParts[0] === 'App') {
120
            return app_path($path);
121
        }
122
123
        // If it it still starts with App, use the left part, otherwise use the whole namespace
124
        // This can be a problem for those using a completely different app path for the application
125
        $left = ($namespaceParts[1] === 'App' ? $namespaceParts[2] : $namespaceParts[0]);
126
127
        // Join, fix slashes for the current operating system, and return path
128
        return app_path(fix_directory_separator(
129
            $left . (filled($path) ? '\\' . $path : '')
130
        ));
131
    }
132
}
133
134
if (!function_exists('make_twill_directory')) {
135
    /**
136
     * @param string $path
137
     * @param bool $recursive
138
     * @param \Illuminate\Filesystem\Filesystem|null $fs
139
     */
140
    function make_twill_directory($path, $recursive = true, $fs = null)
141 1
    {
142
        $fs = filled($fs)
143 1
        ? $fs
144
        : app(Filesystem::class);
145 1
146
        $path = twill_path($path);
147 1
148 1
        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

148
        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...
149
            $fs->makeDirectory($path, 0755, $recursive);
150 1
        }
151
    }
152
}
153
154
if (!function_exists('twill_put_stub')) {
155
    /**
156
     * @param string $path
157
     * @param bool $recursive
158
     * @param \Illuminate\Filesystem\Filesystem|null $fs
159
     */
160
    function twill_put_stub($path, $stub, $fs = null)
161 1
    {
162
        $fs = filled($fs)
163 1
        ? $fs
164
        : app(Filesystem::class);
165 1
166 1
        $stub = str_replace(
167 1
            'namespace App\\',
168 1
            sprintf('namespace %s\\', config('twill.namespace')),
169
            $stub
170
        );
171 1
172 1
        if (!$fs->exists($path)) {
173
            $fs->put($path, $stub);
174 1
        }
175
    }
176
}
177
178
if (!function_exists('fix_directory_separator')) {
179
    /**
180
     * @param string $path
181
     * @param bool $recursive
182
     * @param int $mode
183
     */
184
    function fix_directory_separator($path)
185 55
    {
186 55
        return str_replace(
187 55
            '\\',
188 55
            DIRECTORY_SEPARATOR,
189
            $path
190
        );
191
    }
192
}
193
194
if (!function_exists('generate_list_of_allowed_blocks')) {
195
    /**
196
     * @param array $blocks
197
     * @param array $groups
198
     * @return array
199
     */
200
    function generate_list_of_available_blocks($blocks, $groups)
201
    {
202
        $blockList = app(BlockCollection::class)->getBlockList();
203
204
        $appBlocksList = $blockList->filter(function ($block) {
205
            return $block['source'] !== A17\Twill\Services\Blocks\Block::SOURCE_TWILL;
206
        });
207
208
        return $blockList->filter(
209
            function ($block) use ($blocks, $groups, $appBlocksList) {
210
                if ($block['group'] === A17\Twill\Services\Blocks\Block::SOURCE_TWILL) {
211
                    if (!collect(
212
                        config('twill.block_editor.use_twill_blocks')
213
                    )->contains($block['name'])) {
214
                        return false;
215
                    }
216
217
                    if (count($appBlocksList) > 0 && $appBlocksList->contains(
218
                        function ($appBlock) use ($block) {
219
                            return $appBlock['name'] === $block['name'];
220
                        })
221
                    ) {
222
                        return false;
223
                    }
224
                }
225
226
                return (filled($blocks) ? collect($blocks)->contains($block['name']) : true)
227
                    && (filled($groups) ? collect($groups)->contains($block['group']) : true);
228
            }
229
        )->toArray();
230
    }
231
}
232