1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under New BSD license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @licence New BSD |
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
|
|
|
|
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
|
|
|
$total = count($source); |
|
|
|
|
108
|
|
|
$result = array_splice($source, $start, $end - $start); |
109
|
|
|
Tabs::trim($result); |
110
|
|
|
return implode('', $result); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getPropertySource(ReflectionProperty $property) |
114
|
|
|
{ |
115
|
|
|
$name = []; |
116
|
|
|
|
117
|
|
|
switch (true) |
118
|
|
|
{ |
119
|
|
|
case $property->isPublic(): |
120
|
|
|
$name[] = 'public'; |
121
|
|
|
break; |
122
|
|
|
case $property->isProtected(): |
123
|
|
|
$name[] = 'public'; |
124
|
|
|
break; |
125
|
|
|
case $property->isPrivate(): |
126
|
|
|
$name[] = 'public'; |
127
|
|
|
break; |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
if($property->isStatic()) |
131
|
|
|
{ |
132
|
|
|
$name[] = 'static'; |
133
|
|
|
} |
134
|
|
|
$name[] = sprintf('$%s', $property->name); |
135
|
|
|
|
136
|
|
|
return implode(' ', $name) . ';'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
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.