Completed
Pull Request — master (#68)
by Cristiano
05:36
created

ReflectionUtils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 55
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
namespace gossi\codegen\utils;
18
19
use ReflectionClass;
20
use ReflectionFunctionAbstract;
21
use ReflectionMethod;
22
23
class ReflectionUtils {
24
25 1
	/**
26 1
	 *
27
	 * @param ReflectionClass $class
28 1
	 * @param bool $publicOnly
29 1
	 *
30
	 * @return ReflectionMethod[]
31
	 */
32
	public static function getOverrideableMethods(ReflectionClass $class, bool $publicOnly = false): array {
33 1
		$filter = ReflectionMethod::IS_PUBLIC;
34 1
35
		if (!$publicOnly) {
36
			$filter |= ReflectionMethod::IS_PROTECTED;
37
		}
38
39
		return array_filter($class->getMethods($filter), fn ($method) => !$method->isFinal() && !$method->isStatic());
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_DOUBLE_ARROW, expecting ',' or ')'
Loading history...
40
	}
41 1
42 1
	/**
43 1
	 *
44 1
	 * @param string $docComment
45 1
	 *
46 1
	 * @return string
47
	 */
48
	public static function getUnindentedDocComment(string $docComment): string {
49 1
		$lines = explode("\n", $docComment);
50
		$docBlock = '';
51 1
		for ($i = 0, $c = count($lines); $i < $c; $i++) {
52 1
			if (0 === $i) {
53
				$docBlock = $lines[0] . "\n";
54
				continue;
55
			}
56 1
57
			$docBlock .= ' ' . ltrim($lines[$i]);
58
59
			if ($i + 1 < $c) {
60
				$docBlock .= "\n";
61
			}
62
		}
63 1
64 1
		return $docBlock;
65 1
	}
66 1
67 1
	/**
68 1
	 *
69 1
	 * @param ReflectionFunctionAbstract $function
70 1
	 *
71
	 * @return string
72
	 */
73
	public static function getFunctionBody(ReflectionFunctionAbstract $function): string {
74
		$source = file($function->getFileName());
75
		$start = $function->getStartLine() - 1;
76
		$end = $function->getEndLine();
77
		$body = implode('', array_slice($source, $start, $end - $start));
78
		$open = strpos($body, '{');
79
		$close = strrpos($body, '}');
80
81
		return trim(substr($body, $open + 1, (strlen($body) - $close) * -1));
82
	}
83
}
84