1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of PhpUnitGen. |
5
|
|
|
* |
6
|
|
|
* (c) 2017-2018 Paul Thébaud <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhpUnitGen\Annotation; |
13
|
|
|
|
14
|
|
|
use PhpUnitGen\Annotation\AnnotationInterface\AnnotationInterface; |
15
|
|
|
use PhpUnitGen\Exception\AnnotationParseException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AnnotationFactory. |
19
|
|
|
* |
20
|
|
|
* @author Paul Thébaud <[email protected]>. |
21
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
22
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
23
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
24
|
|
|
* @since Class available since Release 2.0.0. |
25
|
|
|
*/ |
26
|
|
|
class AnnotationFactory |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Build an annotation from a name and a content. |
30
|
|
|
* |
31
|
|
|
* @param string $name The annotation name (such as "@PhpUnitGen\AssertEquals"). |
32
|
|
|
* @param int $line The line number in documentation block. |
33
|
|
|
* |
34
|
|
|
* @return AnnotationInterface The new built annotation. |
35
|
|
|
* |
36
|
|
|
* @throws AnnotationParseException If the annotation is unknown. |
37
|
|
|
*/ |
38
|
|
|
public function invoke(string $name, int $line): AnnotationInterface |
39
|
|
|
{ |
40
|
|
|
$name = preg_replace('/@(?i)(PhpUnitGen|Pug)\\\\/', '', $name); |
41
|
|
|
switch (true) { |
42
|
|
|
case strcasecmp($name, 'get') === 0: |
43
|
|
|
$annotation = new GetAnnotation(); |
44
|
|
|
break; |
45
|
|
|
case strcasecmp($name, 'set') === 0: |
46
|
|
|
$annotation = new SetAnnotation(); |
47
|
|
|
break; |
48
|
|
|
case strcasecmp($name, 'construct') === 0: |
49
|
|
|
$annotation = new ConstructAnnotation(); |
50
|
|
|
break; |
51
|
|
|
case strcasecmp($name, 'mock') === 0: |
52
|
|
|
$annotation = new MockAnnotation(); |
53
|
|
|
break; |
54
|
|
|
case strcasecmp($name, 'params') === 0: |
55
|
|
|
$annotation = new ParamsAnnotation(); |
56
|
|
|
break; |
57
|
|
|
case strcasecmp(substr($name, 0, 6), 'assert') === 0: |
58
|
|
|
$annotation = new AssertAnnotation(); |
59
|
|
|
break; |
60
|
|
|
default: |
61
|
|
|
throw new AnnotationParseException( |
62
|
|
|
sprintf('Annotation of name "%s" is unknown', $name) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
$annotation->setName($name); |
66
|
|
|
$annotation->setLine($line); |
67
|
|
|
return $annotation; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|