Passed
Pull Request — master (#32)
by
unknown
03:09
created

ExpectationAnnotationParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getExpectations() 0 9 2
A getExpectationFullPath() 0 6 2
A parseExpectation() 0 17 3
1
<?php
2
3
4
namespace Codeception\Util;
5
6
7
use Codeception\Configuration;
8
use Codeception\Exception\ParseException;
9
use Codeception\Test\Cest;
10
use Codeception\TestInterface;
11
12
class ExpectationAnnotationParser
13
{
14
    const DEFAULT_EXPECTATIONS_PATH = "tests/_unique_expectations/";
15
    const PATH_CONFIG_KEY = 'unique_expectations_path';
16
    /**
17
     * @var array
18
     */
19
    private $config;
20
21
    /**
22
     * ExpectationAnnotationParser constructor.
23
     *
24
     * @param array $config
25
     *
26
     * @throws \Codeception\Exception\ConfigurationException
27
     */
28
    public function __construct($config = [])
29
    {
30
        if(empty($config)){
31
            $config = Configuration::config();
32
        }
33
        $this->config = $config;
34
    }
35
36
37
    /**
38
     * @param TestInterface|Cest $test
39
     *
40
     * @return array
41
     */
42
    public function getExpectations(TestInterface $test)
43
    {
44
        if(!$test instanceof Cest){
45
            return [];
46
        }
47
        $expectations = Annotation::forMethod($test->getTestClass(), $test->getTestMethod())->fetchAll('expectation');
48
49
        return array_map([$this, 'parseExpectation'], $expectations);
50
    }
51
52
    /**
53
     * @param       $expectationsPath
54
     * @param array $matches
0 ignored issues
show
Bug introduced by
There is no parameter named $matches. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     *
56
     * @return string
57
     */
58
    function getExpectationFullPath($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
    {
60
        $expectationsPath = isset($this->config['paths'][self::PATH_CONFIG_KEY]) ? $this->config['paths'][self::PATH_CONFIG_KEY] : self::DEFAULT_EXPECTATIONS_PATH;
61
62
        return codecept_root_dir($expectationsPath . $path);
63
    }
64
65
    /**
66
     * @param $expectation
67
     *
68
     * @return string
69
     * @throws ParseException
70
     */
71
    public function parseExpectation($expectation)
72
    {
73
        $matches = [];
74
        $expectationRegex = '/\(?\"?(?<filePath>[a-zA-Z0-9_]+)(.json)?\"?\)?/';
75
        preg_match($expectationRegex, $expectation, $matches);
76
77
        if (empty($matches)) {
78
            throw new ParseException("The 'expectation' annotation could not be parsed (found: '$expectation')");
79
        }
80
81
        $expectationPath = $this->getExpectationFullPath("{$matches['filePath']}.json");
82
        if(!file_exists($expectationPath)){
83
            throw new ParseException("The expectation at $expectationPath could not be found ");
84
        }
85
86
        return $expectationPath;
87
    }
88
}