Completed
Push — master ( f86360...4e757b )
by Peter
03:09
created

Source::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
$total is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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