Namer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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
 * @link https://maslosoft.com/zamm/
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
		assert(!empty($className), static::class . ' requires existing class name as a constructor parameter');
48
		$this->className = $className;
49
		$this->info = new ReflectionClass($this->className);
50
		$this->link = new ApiUrl($className);
51
	}
52
53
	/**
54
	 * Set wrapper defaults
55
	 * @return InlineWrapper
56
	 */
57
	public static function defaults()
58
	{
59
		return InlineWrapper::defaults();
60
	}
61
62
	public function __get($name)
63
	{
64
		// This is to get class name formatted, without invoking property()
65
		if ($name === 'md' || $name === 'html' || $name === 'short')
66
		{
67
			if (!$this->info->hasProperty($name))
68
			{
69
				return (new InlineWrapper($this->_type(), (string) $this->link, $this->getTitle()))->$name;
70
			}
71
		}
72
		return $this->_get($name);
73
	}
74
75
	/**
76
	 *
77
	 * @param string $name
78
	 * @return InlineWrapper
79
	 */
80
	public function method($name)
81
	{
82
		assert($this->info->hasMethod($name), "Tried to get non existing method: `$name` info of " . $this->_type());
83
		$link = $this->link->method($name);
84
		return new InlineWrapper($this->_method($name), $link, $this->getTitle("$name()"));
85
	}
86
87
	protected function _method($name)
88
	{
89
		return sprintf('%s::%s()', $this->className, $name);
90
	}
91
92
	/**
93
	 *
94
	 * @param string $name
95
	 * @return InlineWrapper
96
	 */
97
	public function property($name)
98
	{
99
		// Workaround for __getting html link for type.
100
		// Should be trapped in __get, but it doesn't always do.
101
		if ($name === 'md' || $name === 'html' || $name === 'short')
102
		{
103
			if (!$this->info->hasProperty($name))
104
			{
105
				return (new InlineWrapper($this->_type(), (string) $this->link, $this->getTitle()))->$name;
106
			}
107
		}
108
109
110
		assert($this->info->hasProperty($name));
111
		$link = $this->link->property($name);
112
		return new InlineWrapper($this->_property($name), $link, $this->getTitle($name));
113
	}
114
115
	protected function _property($name)
116
	{
117
		return sprintf('%s::%s', $this->className, $name);
118
	}
119
120
	public static function __callStatic($name, $arguments)
121
	{
122
		return new InlineWrapper(sprintf('%s', $name));
123
	}
124
125
	protected function _type()
126
	{
127
		return $this->className;
128
	}
129
130
	public function __toString()
131
	{
132
		$link = (string) $this->link;
133
		return (string) new InlineWrapper($this->_type(), $link, $this->getTitle());
134
	}
135
136
	private function getTitle($name = '')
137
	{
138
		if (!empty($name))
139
		{
140
			return sprintf('%s::%s', $this->info->name, $name);
141
		}
142
		return $this->info->name;
143
	}
144
145
}
146