1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Zamm; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Zamm\Helpers\InlineWrapper; |
16
|
|
|
use Maslosoft\Zamm\Interfaces\SourceAccessorInterface; |
17
|
|
|
use Maslosoft\Zamm\Traits\SourceMagic; |
18
|
|
|
use ReflectionClass; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This simply return names of methods and properties. |
22
|
|
|
* This is helper for IDE's. |
23
|
|
|
* Use this together with @var type hint. |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class Namer implements SourceAccessorInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
use SourceMagic; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Working class name |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $className = ''; |
37
|
|
|
protected $info = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @var ApiUrl |
42
|
|
|
*/ |
43
|
|
|
protected $link = null; |
44
|
|
|
|
45
|
|
|
public function __construct($className = null) |
46
|
|
|
{ |
47
|
|
|
$this->className = $className; |
48
|
|
|
$this->info = new ReflectionClass($this->className); |
49
|
|
|
$this->link = new ApiUrl($className); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set wrapper defaults |
54
|
|
|
* @return InlineWrapper |
55
|
|
|
*/ |
56
|
|
|
public static function defaults() |
57
|
|
|
{ |
58
|
|
|
return InlineWrapper::defaults(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function __get($name) |
62
|
|
|
{ |
63
|
|
|
// This is to get class name formatted, without invoking property() |
64
|
|
View Code Duplication |
if ($name === 'md' || $name === 'html' || $name === 'short') |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (!$this->info->hasProperty($name)) |
67
|
|
|
{ |
68
|
|
|
return (new InlineWrapper($this->_type(), (string) $this->link, $this->getTitle()))->$name; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $this->_get($name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @return InlineWrapper |
78
|
|
|
*/ |
79
|
|
|
public function method($name) |
80
|
|
|
{ |
81
|
|
|
assert($this->info->hasMethod($name), "Tried to get non existing method: `$name` info of " . $this->_type()); |
82
|
|
|
$link = $this->link->method($name); |
83
|
|
|
return new InlineWrapper($this->_method($name), $link, $this->getTitle("$name()")); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function _method($name) |
87
|
|
|
{ |
88
|
|
|
return sprintf('%s::%s()', $this->className, $name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* @return InlineWrapper |
95
|
|
|
*/ |
96
|
|
|
public function property($name) |
97
|
|
|
{ |
98
|
|
|
// Workaround for __getting html link for type. |
99
|
|
|
// Should be trapped in __get, but it doesn't always do. |
100
|
|
View Code Duplication |
if ($name === 'md' || $name === 'html' || $name === 'short') |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
if (!$this->info->hasProperty($name)) |
103
|
|
|
{ |
104
|
|
|
return (new InlineWrapper($this->_type(), (string) $this->link, $this->getTitle()))->$name; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
assert($this->info->hasProperty($name)); |
110
|
|
|
$link = $this->link->property($name); |
111
|
|
|
return new InlineWrapper($this->_property($name), $link, $this->getTitle($name)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function _property($name) |
115
|
|
|
{ |
116
|
|
|
return sprintf('%s::%s', $this->className, $name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function __callStatic($name, $arguments) |
120
|
|
|
{ |
121
|
|
|
return new InlineWrapper(sprintf('%s', $name)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function _type() |
125
|
|
|
{ |
126
|
|
|
return $this->className; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function __toString() |
130
|
|
|
{ |
131
|
|
|
$link = (string) $this->link; |
132
|
|
|
return (string) new InlineWrapper($this->_type(), $link, $this->getTitle()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function getTitle($name = '') |
136
|
|
|
{ |
137
|
|
|
if (!empty($name)) |
138
|
|
|
{ |
139
|
|
|
return sprintf('%s::%s', $this->info->name, $name); |
140
|
|
|
} |
141
|
|
|
return $this->info->name; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.