-
Notifications
You must be signed in to change notification settings - Fork 57
/
ClassAnalyzer.php
71 lines (55 loc) · 1.92 KB
/
ClassAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php declare(strict_types=1);
namespace ObjectCalisthenics\Helper;
use ObjectCalisthenics\Exception\Helper\NonClassTypeTokenTypeException;
use PHP_CodeSniffer\Files\File;
final class ClassAnalyzer
{
/**
* @var mixed[]
*/
private static $propertyList = [];
public static function getClassMethodCount(File $file, int $position): int
{
self::ensureIsClassTraitOrInterface($file, $position);
$methodCount = 0;
$pointer = $position;
while (($next = $file->findNext(T_FUNCTION, $pointer + 1)) !== false) {
++$methodCount;
$pointer = $next;
}
return $methodCount;
}
public static function getClassPropertiesCount(File $file, int $position): int
{
return count(self::getClassProperties($file, $position));
}
/**
* @return mixed[]
*/
public static function getClassProperties(File $file, int $position): array
{
$tokens = $file->getTokens();
$token = $tokens[$position];
$pointer = $token['scope_opener'];
self::$propertyList = [];
while (($pointer = $file->findNext(T_VARIABLE, ($pointer + 1), $token['scope_closer'])) !== false) {
self::extractPropertyIfFound($file, (int) $pointer);
}
return self::$propertyList;
}
private static function extractPropertyIfFound(File $file, int $position): void
{
if (PropertyHelper::isProperty($file, $position)) {
self::$propertyList[] = $position;
}
}
private static function ensureIsClassTraitOrInterface(File $file, int $position): void
{
$token = $file->getTokens()[$position];
if (! in_array($token['code'], [T_CLASS, T_INTERFACE, T_TRAIT], true)) {
throw new NonClassTypeTokenTypeException(
sprintf('Must be class, interface or trait. "%s" given.', ltrim($token['type'], 'T_'))
);
}
}
}