Completed
Push — develop ( a98fd2...49ed8e )
by Jaap
09:10
created

BaseContext   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A gatherContexts() 0 6 1
A findClassByFqsen() 0 14 4
A getAst() 0 12 2
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
18
class BaseContext
19
{
20
    /** @var EnvironmentContext */
21
    private $environmentContext;
22
23
    /** @BeforeScenario */
24
    public function gatherContexts(BeforeScenarioScope $scope)
25
    {
26
        $environment = $scope->getEnvironment();
27
28
        $this->environmentContext = $environment->getContext('phpDocumentor\Behat\Contexts\EnvironmentContext');
29
    }
30
31
    /**
32
     * @param string $classFqsen
33
     * @return ClassDescriptor
34
     * @throws \Exception
35
     */
36
    protected function findClassByFqsen($classFqsen)
37
    {
38
        $ast = $this->getAst();
39
        foreach ($ast->getFiles() as $file) {
40
            /** @var ClassDescriptor $classDescriptor */
41
            foreach ($file->getClasses() as $classDescriptor) {
42
                if ($classDescriptor->getFullyQualifiedStructuralElementName() === $classFqsen) {
43
                    return $classDescriptor;
44
                }
45
            }
46
        }
47
48
        throw new \Exception(sprintf('Didn\'t find expected class "%s"', $classFqsen));
49
    }
50
51
    /**
52
     * @return ProjectDescriptor|null
53
     * @throws \Exception when AST file doesn't exist
54
     */
55
    protected function getAst()
56
    {
57
        $file = $this->environmentContext->getWorkingDir() . '/ast.dump';
58
        if (!file_exists($file)) {
59
            throw new \Exception(
60
                'The output of phpDocumentor was not generated, this probably means that the execution failed. '
61
                . 'The error output was: ' . $this->environmentContext->getErrorOutput()
62
            );
63
        }
64
65
        return unserialize(file_get_contents($file));
66
    }
67
}
68