Passed
Pull Request — main (#1)
by Michael
03:10
created

AutoBindsToContainer::prepareFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Traits;
6
7
use Illuminate\Support\Str;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
trait AutoBindsToContainer
11
{
12
    /**
13
     * Run the folders scanning & bind the results.
14
     *
15
     * @return void
16
     */
17 6
    protected function run(): void
18
    {
19 6
        collect($this->getFolderFiles())
0 ignored issues
show
Bug introduced by
$this->getFolderFiles() of type Symfony\Component\Finder\SplFileInfo[]|array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

19
        collect(/** @scrutinizer ignore-type */ $this->getFolderFiles())
Loading history...
20 6
            ->each(function (SplFileInfo $file) {
21 6
                $relativePath             = $file->getRelativePathname();
22 6
                $filenameWithoutExtension = $file->getFilenameWithoutExtension();
23 6
                $filenameWithRelativePath = $this->prepareFilename($relativePath);
24
25 6
                $interface = $this->interfaceFrom($filenameWithoutExtension);
26 6
                $concrete  = $this->concreteFrom($filenameWithRelativePath);
27
28 6
                app()->{$this->bindingType}($interface, $concrete);
29
            });
30
    }
31
32
    /**
33
     * Get the folder files.
34
     *
35
     * @return array
36
     */
37 6
    protected function getFolderFiles(): array
38
    {
39 6
        $filesystem = app('files');
40
41 6
        $folder = base_path($this->basePath . DIRECTORY_SEPARATOR . $this->classFolder);
42
43 6
        return $filesystem->isDirectory($folder)
44 6
            ? $filesystem->allFiles($folder)
45 6
            : [];
46
    }
47
48
    /**
49
     * Prepare the filename.
50
     *
51
     * @param string $filename
52
     *
53
     * @return string
54
     */
55 6
    protected function prepareFilename(string $filename): string
56
    {
57 6
        return (string) Str::of($filename)
58 6
            ->replace('/', '\\')
59 6
            ->substr(0, (int) strrpos($filename, '.'));
60
    }
61
62
    /**
63
     * Get the namespace from a given path.
64
     *
65
     * @param string $path
66
     *
67
     * @return string
68
     */
69 6
    protected function namespaceFrom(string $path): string
70
    {
71 6
        return (string) Str::of($path)
72 6
            ->replace('/', '\\')
73 6
            ->ucfirst();
74
    }
75
76
    /**
77
     * Get the interface from filename.
78
     *
79
     * @param string $filenameWithoutExtension
80
     *
81
     * @return string
82
     */
83 6
    protected function interfaceFrom(string $filenameWithoutExtension): string
84
    {
85 6
        $interface = $this->guessInterfaceWith($filenameWithoutExtension);
86
87 6
        if (is_null($interface)) {
88 4
            return $this->interfaceNamespace
89
                . '\\'
90
                . $filenameWithoutExtension
91 4
                . ($this->interfacePostfix);
92
        }
93
94 2
        return $interface;
95
    }
96
97
    /**
98
     * Guess the interface with a given filename.
99
     *
100
     * @param string $filenameWithoutExtension
101
     *
102
     * @return string|null
103
     */
104 6
    protected function guessInterfaceWith(string $filenameWithoutExtension): ?string
105
    {
106 6
        if (! Str::contains($this->interfaceNamespace, '\\')) {
107 2
            return $this->classNamespace
108
                . '\\'
109 2
                . $this->classFolder
110
                . '\\'
111 2
                . $this->interfaceNamespace
112
                . '\\'
113
                . $filenameWithoutExtension
114 2
                . ($this->interfacePostfix);
115
        }
116
117 4
        return null;
118
    }
119
120
    /**
121
     * Get the concrete from filename.
122
     *
123
     * @param string $filenameWithRelativePath
124
     *
125
     * @return string
126
     */
127 6
    protected function concreteFrom(string $filenameWithRelativePath): string
128
    {
129 6
        return $this->classNamespace . '\\' . $this->classFolder . '\\' . $filenameWithRelativePath;
130
    }
131
}
132