Passed
Push — develop ( 5def3e...6cdbbb )
by Paul
02:27
created

AnnotationFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B invoke() 0 27 6
1
<?php
2
3
namespace PhpUnitGen\Annotation;
4
5
use PhpUnitGen\Annotation\AnnotationInterface\AnnotationInterface;
6
use PhpUnitGen\Exception\AnnotationParseException;
7
8
/**
9
 * Class AnnotationFactory.
10
 *
11
 * @author     Paul Thébaud <[email protected]>.
12
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
13
 * @license    https://opensource.org/licenses/MIT The MIT license.
14
 * @link       https://github.com/paul-thebaud/phpunit-generator
15
 * @since      Class available since Release 2.0.0.
16
 */
17
class AnnotationFactory
18
{
19
    /**
20
     * Build an annotation from a name and a content.
21
     *
22
     * @param string $name The annotation name (such as "@PhpUnitGen\AssertEquals").
23
     * @param int    $line The line number in documentation block.
24
     *
25
     * @return AnnotationInterface The new built annotation.
26
     *
27
     * @throws AnnotationParseException If the annotation is unknown.
28
     */
29
    public function invoke(string $name, int $line): AnnotationInterface
30
    {
31
        $name = preg_replace('/@(?i)(PhpUnitGen|Pug)\\\\/', '', $name);
32
        switch (true) {
33
            case strcasecmp($name, 'getter') === 0:
34
                $annotation = new GetterAnnotation();
35
                break;
36
            case strcasecmp($name, 'setter') === 0:
37
                $annotation = new SetterAnnotation();
38
                break;
39
            case strcasecmp($name, 'constructor') === 0:
40
                $annotation = new ConstructorAnnotation();
41
                break;
42
            case strcasecmp($name, 'mock') === 0:
43
                $annotation = new MockAnnotation();
44
                break;
45
            case strcasecmp(substr($name, 0, 6), 'assert') === 0:
46
                $annotation = new AssertionAnnotation();
47
                break;
48
            default:
49
                throw new AnnotationParseException(
50
                    sprintf('Annotation of name "%s" is unknown', $name)
51
                );
52
        }
53
        $annotation->setName($name);
54
        $annotation->setLine($line);
55
        return $annotation;
56
    }
57
}
58