Completed
Push — master ( 1c06cf...c6b670 )
by Peter
11:21
created

Source::getTypeSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Tabs;
16
use Maslosoft\Zamm\Helpers\Wrapper;
17
use Maslosoft\Zamm\Interfaces\SourceAccessorInterface;
18
use ReflectionClass;
19
use ReflectionProperty;
20
21
/**
22
 * Source extractor for code fragments.
23
 * This class extracts source code for specified code elements.
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class Source implements SourceAccessorInterface
28
{
29
30
	use Traits\SourceMagic;
31
32
	/**
33
	 * Reflection information
34
	 * @var ReflectionClass
35
	 */
36
	private $info = null;
37
38
	/**
39
	 * Source as array
40
	 * @var string[]
41
	 */
42
	private $source = [];
43
44
	public function __construct($className = null)
45
	{
46
		$this->info = new ReflectionClass($className);
47
		$this->source = file($this->info->getFileName());
48
	}
49
50
	/**
51
	 * Should return source of working class
52
	 */
53
	public function __toString()
54
	{
55
		return $this->getTypeSource();
56
	}
57
58
	public static function __callStatic($name, $arguments)
59
	{
60
		
61
	}
62
63
	/**
64
	 * Get source code of method
65
	 * @param string $name
66
	 * @return Wrapper
67
	 */
68 View Code Duplication
	public function method($name)
69
	{
70
		$method = $this->info->getMethod($name);
71
		$code = $this->getMethodSource($method->getStartLine(), $method->getEndLine());
72
		$result = [];
73
		$comment = $method->getDocComment();
74
		Tabs::trim($comment);
75
		$result[] = $comment . PHP_EOL;
76
		$result[] = $code . PHP_EOL;
77
		return new Wrapper(implode('', $result));
78
	}
79
80
	/**
81
	 * Get source code of property
82
	 * @param string $name
83
	 * @return Wrapper
84
	 */
85 View Code Duplication
	public function property($name)
86
	{
87
		$property = $this->info->getProperty($name);
88
		$code = $this->getPropertySource($property);
89
		$result = [];
90
		$comment = $property->getDocComment();
91
		Tabs::trim($comment);
92
		$result[] = $comment . PHP_EOL;
93
		$result[] = $code . PHP_EOL;
94
95
		return new Wrapper(implode('', $result));
96
	}
97
98
	private function getMethodSource($start, $end)
99
	{
100
		// Assume blank line after method name
101
		// So start one line above
102
		if (preg_match('~^\s*\{~', $this->source[$start]))
103
		{
104
			$start--;
105
		}
106
		$source = array_values($this->source);
107
		$result = array_splice($source, $start, $end - $start);
108
		Tabs::trim($result);
109
		return implode('', $result);
110
	}
111
112
	public function getPropertySource(ReflectionProperty $property)
113
	{
114
		$name = [];
115
116
		switch (true)
117
		{
118
			case $property->isPublic():
119
				$name[] = 'public';
120
				break;
121
			case $property->isProtected():
122
				$name[] = 'public';
123
				break;
124
			case $property->isPrivate():
125
				$name[] = 'public';
126
				break;
127
		}
128
		if ($property->isStatic())
129
		{
130
			$name[] = 'static';
131
		}
132
		$name[] = sprintf('$%s', $property->name);
133
134
		return implode(' ', $name) . ';';
135
	}
136
137
	private function getTypeSource()
138
	{
139
		return '';
140
	}
141
142
}
143