ResolvesClasses::resolveClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Traits;
4
5
use Closure;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
trait ResolvesClasses
9
{
10
    /**
11
     * Resolves and Initializes Class by filename.
12
     *
13
     * @param SplFileInfo $fileInfo
14
     * @param null|Closure $callback
15
     */
16
    public function resolveClass(SplFileInfo $fileInfo, Closure $callback = null)
17
    {
18
        /** @noinspection PhpIncludeInspection */
19
        require_once $fileInfo->getPathname();
20
21
        $instance = app()->make($this->getModuleClassName($fileInfo));
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

21
        $instance = /** @scrutinizer ignore-call */ app()->make($this->getModuleClassName($fileInfo));
Loading history...
22
23
        if (\is_callable($callback)) {
24
            $callback($instance);
25
        }
26
    }
27
28
    /**
29
     * @param SplFileInfo $fileInfo
30
     *
31
     * @return mixed
32
     */
33
    protected function getModuleClassName(SplFileInfo $fileInfo)
34
    {
35
        $name = str_replace('.php', '', $fileInfo->getBasename());
36
37
        $path = trim(str_replace(
38
            app_path(),
0 ignored issues
show
Bug introduced by
The function app_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

38
            /** @scrutinizer ignore-call */ 
39
            app_path(),
Loading history...
39
            '',
40
            \dirname($fileInfo->getPathname())
41
        ), \DIRECTORY_SEPARATOR);
42
43
        $location = str_replace('/', '\\', $path);
44
45
        return app()->getNamespace()."{$location}\\{$name}";
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

45
        return /** @scrutinizer ignore-call */ app()->getNamespace()."{$location}\\{$name}";
Loading history...
46
    }
47
}
48