Completed
Push — develop ( b902d9...86bf56 )
by Jaap
05:59
created

UsesTagContext::hasUsesTagReference()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 11
rs 9.9
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-2018 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
use Behat\Behat\Context\Context;
16
use Behat\Gherkin\Node\PyStringNode;
17
use Exception;
18
use phpDocumentor\Descriptor\Tag\UsesDescriptor;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * This class contains the context methods for tests of the uses tag.
23
 */
24
final class UsesTagContext extends BaseContext implements Context
25
{
26
    /**
27
     * @param string $classFqsen
28
     * @throws Exception
29
     * @Then class ":classFqsen" has a tag uses referencing url ":reference"
30
     */
31
    public function classHasTagUsesReferencingUrl($classFqsen, $reference)
32
    {
33
        $class = $this->findClassByFqsen($classFqsen);
34
        $usesTags = $class->getTags()->get('uses', []);
35
        $this->hasUsesTagReference($usesTags, $reference);
36
    }
37
38
    /**
39
     * @param string $classFqsen
40
     * @Then class ":classFqsen" has :number tag/tags uses referencing :element descriptor ":reference"
41
     */
42
    public function classHasTagUsesReferencing($classFqsen, $number, $element, $reference)
43
    {
44
        $this->classHasTagUsesReferencingWithDescription($classFqsen, $number, $element, $reference, new PyStringNode([], 0));
45
    }
46
47
    /**
48
     * @param string $classFqsen
49
     * @throws Exception
50
     * @Then class ":classFqsen" has :number tag/tags uses referencing :element descriptor ":reference" with description:
51
     */
52
    public function classHasTagUsesReferencingWithDescription($classFqsen, $number, $element, $reference, PyStringNode $description)
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        $count = 0;
55
        $class = $this->findClassByFqsen($classFqsen);
56
        $usesTags = $class->getTags()->get('uses', []);
57
        /** @var UsesTag $tag */
58
        foreach ($usesTags as $tag) {
59
            $r = (string) $tag->getReference();
60
            if ($r === $reference
61
                && ((string) $tag->getDescription()) === $description->getRaw()
62
            ) {
63
                ++$count;
64
            }
65
        }
66
67
        Assert::eq($number, $count, sprintf('Missing uses tag with reference "%s"', $reference));
68
    }
69
70
71
    /**
72
     * @then function ":function" has tag uses referencing ":reference"
73
     * @throws Exception
74
     */
75
    public function functionHasUsesTagReferencing(string $function, string $reference)
76
    {
77
        $functionDescriptor = $this->findFunctionByFqsen($function);
78
        $this->hasUsesTagReference($functionDescriptor->getTags()->get('uses', []), $reference);
79
    }
80
81
    /**
82
     * @param $reference
83
     * @param UsesDescriptor[] $usesTags
84
     * @throws Exception
85
     */
86
    private function hasUsesTagReference($usesTags, string $reference) : void
87
    {
88
        /** @var UsesDescriptor $tag */
89
        foreach ($usesTags as $tag) {
90
            if (((string) $tag->getReference()) === $reference) {
91
                return;
92
            }
93
        }
94
95
        throw new Exception(sprintf('Missing uses tag with reference "%s"', $reference));
96
    }
97
}
98