Completed
Push — master ( 63d317...a38ccb )
by Panagiotis
04:27
created

FaultReflector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reflectFromClassName() 0 3 1
A reflectFromFile() 0 15 1
A loadClass() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Author: panosru
6
 * Date: 2019-01-13
7
 * Time: 11:32
8
 */
9
10
namespace Omega\FaultManager\Traits;
11
12
use Roave\BetterReflection\BetterReflection;
13
use Roave\BetterReflection\Reflection\ReflectionClass;
14
use PhpParser\PrettyPrinter\Standard as CodePrinter;
15
use Roave\BetterReflection\Reflector\ClassReflector;
16
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
17
use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator;
18
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
19
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
20
21
/**
22
 * Trait FaultReflector
23
 * @package Omega\FaultManager\Traits
24
 * @codeCoverageIgnore
25
 */
26
trait FaultReflector
27
{
28
    /**
29
     * @param string $className
30
     * @return ReflectionClass
31
     */
32
    protected static function reflectFromClassName(string $className): ReflectionClass
33
    {
34
        return (new BetterReflection())->classReflector()->reflect($className);
35
    }
36
37
    /**
38
     * @param string $path
39
     * @return ReflectionClass
40
     */
41
    protected static function reflectFromFile(string $path): ReflectionClass
42
    {
43
        $astLocator = (new BetterReflection())->astLocator();
44
        $reflector = new ClassReflector(new AggregateSourceLocator([
45
            new SingleFileSourceLocator($path, $astLocator),
46
            new ComposerSourceLocator((require __DIR__ . '/../../vendor/autoload.php'), $astLocator),
47
            new PhpInternalSourceLocator($astLocator)
48
        ]));
49
        $className = $reflector->getAllClasses()[0]->getName();
50
        /** @var ReflectionClass $reflection */
51
        $reflection = $reflector->reflect($className);
52
53
        self::loadClass($reflection);
54
55
        return $reflection;
56
    }
57
58
    /**
59
     * @param ReflectionClass $reflection
60
     */
61
    private static function loadClass(ReflectionClass $reflection): void
62
    {
63
        eval((new CodePrinter())->prettyPrint([$reflection->getAst()]));
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
64
    }
65
}
66