ClassOpener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 12
dl 0
loc 69
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A open() 0 8 1
A create() 0 14 1
A getVisitors() 0 6 1
1
<?php
2
3
namespace Kambo\Testing\ClassOpener;
4
5
use Kambo\Testing\ClassOpener\ClassManipulation\Node\Visitor\RemoveFinal;
6
7
use Kambo\Testing\ClassOpener\ClassManipulation\Locator\Reflection;
8
use Kambo\Testing\ClassOpener\ClassManipulation\Reader\PhpParser;
9
use Kambo\Testing\ClassOpener\ClassManipulation\Patcher\EvalExecution;
10
use Kambo\Testing\ClassOpener\ClassManipulation\Transformer\Traverser;
11
use Kambo\Testing\ClassOpener\ClassManipulation\Node\Traverser as NodeTraverser;
12
13
use Kambo\Testing\ClassOpener\ClassManipulation\Reader;
14
use Kambo\Testing\ClassOpener\ClassManipulation\Patcher;
15
use Kambo\Testing\ClassOpener\ClassManipulation\Transformer;
16
17
use BetterReflection\SourceLocator\Ast\Locator as AstLocator;
18
use BetterReflection\SourceLocator\Type\AutoloadSourceLocator;
19
use BetterReflection\Reflector\ClassReflector;
20
21
/**
22
 * Open the final class for the further modification eg.: for mocking
23
 *
24
 * @author  Bohuslav Simek <[email protected]>
25
 * @license MIT
26
 */
27
class ClassOpener
28
{
29
    private $reader;
30
    private $transformer;
31
    private $patcher;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param Reader      $reader      Source code class reader, which will parse code into AST.
37
     * @param Transformer $transformer Transform class AST.
38
     * @param Patcher     $patcher     Patch class with its modified version.
39
     */
40 4
    public function __construct(Reader $reader, Transformer $transformer, Patcher $patcher)
41
    {
42 4
        $this->reader = $reader;
43 4
        $this->transformer = $transformer;
44 4
        $this->patcher = $patcher;
45 4
    }
46
47
    /**
48
     * Open the final class for the further modification
49
     *
50
     * @param string $className Name of the class, a fully qualified class name must be provided
51
     *                          eg.: \foo\bar\qaz
52
     *
53
     * @return void
54
     */
55 4
    public function open(string $className)
56
    {
57 4
        $classAst = $this->reader->getNodes($className);
58
59 3
        $this->transformer->transform($className, $classAst, $this->getVisitors());
60
61 3
        $this->patcher->patch($classAst);
62 3
    }
63
64
    /**
65
     * Create a new instance of the class with the default dependencies.
66
     *
67
     * @return self A new instance of self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69 4
    public static function create() : self
70
    {
71 4
        $astLocator = new AstLocator;
72 4
        $locator = new AutoloadSourceLocator($astLocator);
73 4
        $reflector = new ClassReflector($locator);
74
75 4
        $reflectionLocator = new Reflection($reflector);
76
77 4
        $reader = new PhpParser($reflectionLocator);
78 4
        $transformer = new Traverser(new NodeTraverser);
79 4
        $patcher = new EvalExecution;
80
81 4
        return new self($reader, $transformer, $patcher);
82
    }
83
84
    /**
85
     * Get all visitors which will be applied to the class
86
     *
87
     * @return array
88
     */
89 3
    private function getVisitors() : array
90
    {
91
        return [
92 3
            new RemoveFinal
93
        ];
94
    }
95
}
96