Completed
Push — master ( 9b18dc...e19264 )
by Tomáš
18s
created

FunctionHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 55
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B findReturnTypeHint() 0 33 6
A isAbstract() 0 12 3
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace ZenifyCodingStandard\Helper\Commenting;
11
12
use PHP_CodeSniffer_File;
13
14
15
/**
16
 * Inspired by https://github.com/slevomat/coding-standard/blob/4f81f58625bf86bd91f7fc6f8e4d12160bf03c7c/SlevomatCodingStandard/Helpers/FunctionHelper.php
17
 */
18
final class FunctionHelper
19
{
20
21
	/**
22
	 * @return string|NULL
23
	 */
24
	public static function findReturnTypeHint(PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer)
25
	{
26
		$tokens = $codeSnifferFile->getTokens();
27
		$isAbstract = self::isAbstract($codeSnifferFile, $functionPointer);
28
		$colonToken = $isAbstract
29
			? $codeSnifferFile->findNext(
30
				T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, NULL, FALSE, NULL, TRUE
31
			)
32
			: $codeSnifferFile->findNext(
33
				T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, $tokens[$functionPointer]['scope_opener'] - 1
34
			);
35
36
		if ($colonToken === FALSE) {
37
			return NULL;
38
		}
39
		$returnTypeHint = NULL;
40
		$nextToken = $colonToken;
41
42
		do {
43
			$nextToken = $isAbstract
44
				? $codeSnifferFile->findNext([T_WHITESPACE, T_COMMENT, T_SEMICOLON], $nextToken + 1, NULL, TRUE, NULL, TRUE)
45
				: $codeSnifferFile->findNext(
46
					[T_WHITESPACE, T_COMMENT], $nextToken + 1, $tokens[$functionPointer]['scope_opener'] - 1, TRUE
47
				);
48
49
			$isTypeHint = $nextToken !== FALSE;
50
			if ($isTypeHint) {
51
				$returnTypeHint .= $tokens[$nextToken]['content'];
52
			}
53
		} while ($isTypeHint);
54
55
		return $returnTypeHint;
56
	}
57
58
59
	public static function isAbstract(PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): bool
60
	{
61
		if ( ! isset($codeSnifferFile->getTokens()[$functionPointer]['scope_opener'])) {
62
			return FALSE;
63
		}
64
65
		if ($codeSnifferFile->getTokens()[$functionPointer]['scope_opener'] >= 39) {
66
			return TRUE;
67
		}
68
69
		return FALSE;
70
	}
71
72
}
73