Completed
Push — develop ( e58221...7a35e1 )
by Jaap
08:17 queued 01:05
created

BaseContext::findFunctionByFqsen()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2017 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Behat\Contexts\Ast;
14
15
16
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
17
use phpDocumentor\Descriptor\ClassDescriptor;
18
use phpDocumentor\Descriptor\FunctionDescriptor;
19
use phpDocumentor\Descriptor\ProjectDescriptor;
20
21
class BaseContext
22
{
23
    /** @var EnvironmentContext */
24
    private $environmentContext;
25
26
    /** @BeforeScenario */
27
    public function gatherContexts(BeforeScenarioScope $scope)
28
    {
29
        $environment = $scope->getEnvironment();
30
31
        $this->environmentContext = $environment->getContext('phpDocumentor\Behat\Contexts\EnvironmentContext');
32
    }
33
34
    /**
35
     * @param string $classFqsen
36
     * @return ClassDescriptor
37
     * @throws \Exception
38
     */
39
    protected function findClassByFqsen($classFqsen): ClassDescriptor
40
    {
41
        $ast = $this->getAst();
42
        foreach ($ast->getFiles() as $file) {
43
            /** @var ClassDescriptor $classDescriptor */
44
            foreach ($file->getClasses() as $classDescriptor) {
45
                if (((string)$classDescriptor->getFullyQualifiedStructuralElementName()) === $classFqsen) {
46
                    return $classDescriptor;
47
                }
48
            }
49
        }
50
51
        throw new \Exception(sprintf('Didn\'t find expected class "%s"', $classFqsen));
52
    }
53
54
    /**
55
     * @param string $fqsen
56
     * @return FunctionDescriptor
57
     * @throws \Exception
58
     */
59
    protected function findFunctionByFqsen(string $fqsen): FunctionDescriptor
60
    {
61
        $ast = $this->getAst();
62
        foreach ($ast->getFiles() as $file) {
63
            /** @var FunctionDescriptor $classDescriptor */
64
            foreach ($file->getFunctions() as $function) {
65
                if ((string)$function->getFullyQualifiedStructuralElementName() === '\\' . $fqsen . '()') {
66
                    return $function;
67
                }
68
            }
69
        }
70
71
        throw new \Exception(sprintf('Didn\'t find expected function "%s"', $fqsen));
72
    }
73
74
    /**
75
     * @return ProjectDescriptor|null
76
     * @throws \Exception when AST file doesn't exist
77
     */
78
    protected function getAst()
79
    {
80
        $file = $this->environmentContext->getWorkingDir() . '/ast.dump';
81
        if (!file_exists($file)) {
82
            throw new \Exception(
83
                'The output of phpDocumentor was not generated, this probably means that the execution failed. '
84
                . 'The error output was: ' . $this->environmentContext->getErrorOutput()
85
            );
86
        }
87
88
        return unserialize(file_get_contents($file));
89
    }
90
91
    protected function processFilePath($file)
92
    {
93
        return $file;
94
    }
95
}
96