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
|
|
|
use PhpUnitGen\Exception\JsonException; |
17
|
|
|
use PhpUnitGen\Util\Json; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class GetAnnotation. |
21
|
|
|
* |
22
|
|
|
* @author Paul Thébaud <[email protected]>. |
23
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
24
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
25
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
26
|
|
|
* @since Class available since Release 2.0.0. |
27
|
|
|
*/ |
28
|
|
|
class GetAnnotation extends AbstractAnnotation |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string $property The name of the property to get. |
32
|
|
|
*/ |
33
|
|
|
private $property; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getType(): int |
39
|
|
|
{ |
40
|
|
|
return AnnotationInterface::TYPE_GET; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function compile(): void |
47
|
|
|
{ |
48
|
|
|
if (strlen($this->getStringContent()) > 0) { |
49
|
|
|
// Decode JSON content |
50
|
|
|
try { |
51
|
|
|
$decoded = Json::decode($this->getStringContent()); |
52
|
|
|
} catch (JsonException $exception) { |
53
|
|
|
throw new AnnotationParseException('"getter" annotation content is invalid (invalid JSON content)'); |
54
|
|
|
} |
55
|
|
|
if (! is_string($decoded)) { |
56
|
|
|
throw new AnnotationParseException( |
57
|
|
|
'"getter" annotation content is invalid (property name must be a string)' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
$this->property = $decoded; |
61
|
|
|
} else { |
62
|
|
|
$this->property = preg_replace( |
63
|
|
|
'/^get/', |
64
|
|
|
'', |
65
|
|
|
$this->getParentNode()->/** @scrutinizer ignore-call */ |
66
|
|
|
getName() |
67
|
|
|
); |
68
|
|
|
$this->property = lcfirst($this->property); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string The name of the property to get. |
74
|
|
|
*/ |
75
|
|
|
public function getProperty(): string |
76
|
|
|
{ |
77
|
|
|
return $this->property; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|