Completed
Pull Request — master (#30)
by
unknown
05:11
created

ReflectionUtils   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 25
c 4
b 0
f 0
lcom 0
cbo 2
dl 0
loc 110
ccs 64
cts 72
cp 0.8889
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctionBody() 0 9 1
A getOverrideableMethods() 0 11 3
A getUnindentedDocComment() 0 17 4
C getUseStatements() 0 54 17
1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
namespace gossi\codegen\utils;
19
20
use phootwork\tokenizer\PhpTokenizer;
21
22
class ReflectionUtils {
23
24
	/**
25
	 *
26
	 * @param bool $publicOnly        	
27
	 */
28 1
	public static function getOverrideableMethods(\ReflectionClass $class, $publicOnly = false) {
29 1
		$filter = \ReflectionMethod::IS_PUBLIC;
30
31 1
		if (!$publicOnly) {
32 1
			$filter |= \ReflectionMethod::IS_PROTECTED;
33 1
		}
34
35
		return array_filter($class->getMethods($filter), function ($method) {
36 1
			return !$method->isFinal() && !$method->isStatic();
37 1
		});
38
	}
39
40
	/**
41
	 *
42
	 * @param string $docComment        	
43
	 */
44 1
	public static function getUnindentedDocComment($docComment) {
45 1
		$lines = explode("\n", $docComment);
46 1
		for ($i = 0, $c = count($lines); $i < $c; $i++) {
47 1
			if (0 === $i) {
48 1
				$docBlock = $lines[0] . "\n";
49 1
				continue;
50
			}
51
52 1
			$docBlock .= ' ' . ltrim($lines[$i]);
53
54 1
			if ($i + 1 < $c) {
55 1
				$docBlock .= "\n";
56 1
			}
57 1
		}
58
59 1
		return $docBlock;
60
	}
61
62
	/**
63
	 *
64
	 * @param \ReflectionFunctionAbstract $function        	
65
	 */
66 4
	public static function getFunctionBody(\ReflectionFunctionAbstract $function) {
67 4
		$source = file($function->getFileName());
68 4
		$start = $function->getStartLine() - 1;
69 4
		$end = $function->getEndLine();
70 4
		$body = implode('', array_slice($source, $start, $end - $start));
71 4
		$open = strpos($body, '{');
72 4
		$close = strrpos($body, '}');
73 4
		return trim(substr($body, $open + 1, (strlen($body) - $close) * -1));
74
	}
75
76 3
	public static function getUseStatements(\ReflectionClass $class) {
77 3
		$content = '';
78 3
		$file = file($class->getFileName());
79 3
		for ($i = 0; $i < $class->getStartLine(); $i++) {
80 3
			$content .= $file[$i];
81 3
		}
82 3
		$tokenizer = new PhpTokenizer();
83 3
		$tokens = $tokenizer->tokenize($content);
84 3
		$tokens = $tokens->filter(function ($token) {
85 3
			return $token->type !== T_WHITESPACE && $token->type !== T_COMMENT && $token->type !== T_DOC_COMMENT;
86 3
		});
87 3
		$statements = [];
88
89 3
		while (($token = $tokens->next())) {
90 3
			if ($token->type === T_USE) {
91 2
				$explicitAlias = false;
92 2
				$alias = '';
93 2
				$class = '';
94
95 2
				while (($token = $tokens->next())) {
96 2
					$isNameToken = $token->type === T_STRING || $token->type === T_NS_SEPARATOR;
97 2
					if (!$explicitAlias && $isNameToken) {
98 2
						$class .= $token->contents;
99 2
					} else if ($explicitAlias && $isNameToken) {
100 1
						$alias .= $token->contents;
101 2
					} else if ($token->type === T_AS) {
102 1
						$explicitAlias = true;
103 1
						$alias = '';
104 2
					} else if ($token->contents === ',') {
105
						if ($explicitAlias) {
106
							$statements[$alias] = $class;
107
						} else {
108
							$statements[] = $class;
109
						}
110
111
						$class = '';
112
						$alias = '';
113
						$explicitAlias = false;
114 2
					} else if ($token->contents === ';') {
115 2
						if ($explicitAlias) {
116 1
							$statements[$alias] = $class;
117 1
						} else {
118 2
							$statements[] = $class;
119
						}
120 2
						break;
121
					} else {
122
						break;
123
					}
124 2
				}
125 2
			}
126 3
		}
127
128 3
		return $statements;
129
	}
130
131
}
132