Passed
Push — main ( a38817...2056a2 )
by Michael
03:05
created

AutoBinds::getFolderFiles()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Traits;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
trait AutoBinds
12
{
13
    /**
14
     * @var string
15
     */
16
    private string $namespace;
17
18
    /**
19
     * @return void
20
     */
21 2
    private function prepareNamespace(): void
22
    {
23 2
        $this->namespace = Str::ucfirst(
24 2
            strtr(config('auto-binder.start_namespace', self::DEFAULT_NAMESPACE), '/', '\\')
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...inds::DEFAULT_NAMESPACE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
        );
26
    }
27
28
    /**
29
     * @param string $folder
30
     *
31
     * @return Collection
32
     */
33 2
    private function getFolderFiles(string $folder): Collection
34
    {
35 2
        $path = base_path(config('auto-binder.start_folder', self::DEFAULT_FOLDER))
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...toBinds::DEFAULT_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
            . DIRECTORY_SEPARATOR
37
            . $folder;
38
39 2
        $filesystem = app('files');
40
41 2
        return collect($filesystem->isDirectory($path) ? $filesystem->allFiles($path) : [])
0 ignored issues
show
Bug introduced by
$filesystem->isDirectory...lFiles($path) : array() 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

41
        return collect(/** @scrutinizer ignore-type */ $filesystem->isDirectory($path) ? $filesystem->allFiles($path) : [])
Loading history...
42 2
            ->reject(fn (SplFileInfo $file) => collect(config('auto-binder.exclude_from_scan', self::DEFAULT_SCAN_EXCLUDES))
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...::DEFAULT_SCAN_EXCLUDES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43 2
            ->map(fn (string $folder) => str_contains($file->getRelativePath(), $folder))
44 2
            ->contains(true));
45
    }
46
47
    /**
48
     * @param string $filename
49
     *
50
     * @return string
51
     */
52 2
    private function cleanupFilename(string $filename): string
53
    {
54 2
        return strtr(
55 2
            substr(
56
                $filename,
57
                0,
58 2
                (int) strrpos($filename, '.')
59
            ),
60
            '/',
61
            '\\'
62
        );
63
    }
64
65
    /**
66
     * @param string $folder
67
     * @param string $filenameWithoutExtension
68
     *
69
     * @return string
70
     */
71 2
    private function getInterface(string $folder, string $filenameWithoutExtension): string
72
    {
73 2
        return $this->namespace
74
            . self::CLASS_SEPARATOR
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...oBinds::CLASS_SEPARATOR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
            . $folder
76
            . self::CLASS_SEPARATOR
77 2
            . (config('auto-binder.interface_folder', self::DEFAULT_INTERFACE_FOLDER))
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...EFAULT_INTERFACE_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
            . self::CLASS_SEPARATOR
79
            . $filenameWithoutExtension
80 2
            . (config('auto-binder.interface_postfix', self::DEFAULT_INTERFACE_POSTFIX));
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...FAULT_INTERFACE_POSTFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
    }
82
83
    /**
84
     * @param string $folder
85
     * @param string $filenameWithRelativePath
86
     *
87
     * @return string
88
     */
89 2
    private function getImplementation(string $folder, string $filenameWithRelativePath): string
90
    {
91 2
        return $this->namespace
92
            . self::CLASS_SEPARATOR
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...oBinds::CLASS_SEPARATOR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
93
            . $folder
94
            . self::CLASS_SEPARATOR
95
            . $filenameWithRelativePath;
96
    }
97
98
    /**
99
     * @param string $interface
100
     * @param string $implementation
101
     *
102
     * @return void
103
     */
104 2
    private function bind(string $interface, string $implementation): void
105
    {
106 2
        app()->{config('auto-binder.binding_type', self::DEFAULT_BINDING_TYPE)}($interface, $implementation);
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\AutoBinder\...s::DEFAULT_BINDING_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
107
    }
108
}
109