Completed
Push — master ( 2a6630...1a6e67 )
by Tomáš
02:50
created

MethodDocBlock   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 50 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 17
loc 34
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMethodDocBlock() 17 17 3
A getMethodDocBlock() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of Zenify
7
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
8
 */
9
10
namespace ZenifyCodingStandard\Helper\Commenting;
11
12
use PHP_CodeSniffer_File;
13
14
15
final class MethodDocBlock
16
{
17
18 1 View Code Duplication
	public static function hasMethodDocBlock(PHP_CodeSniffer_File $file, int $position) : bool
19
	{
20 1
		$tokens = $file->getTokens();
21 1
		$currentToken = $tokens[$position];
22 1
		$docBlockClosePosition = $file->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $position);
23
24 1
		if ($docBlockClosePosition === FALSE) {
25 1
			return FALSE;
26
		}
27
28 1
		$docBlockCloseToken = $tokens[$docBlockClosePosition];
29 1
		if ($docBlockCloseToken['line'] === ($currentToken['line'] - 1)) {
30 1
			return TRUE;
31
		}
32
33
		return FALSE;
34
	}
35
36
37 1
	public static function getMethodDocBlock(PHP_CodeSniffer_File $file, int $position) : string
38
	{
39 1
		if ( ! self::hasMethodDocBlock($file, $position)) {
40 1
			return '';
41
		}
42
43 1
		$commentStart = $file->findPrevious(T_DOC_COMMENT_OPEN_TAG, $position - 1);
44 1
		$commentEnd = $file->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $position - 1);
45 1
		return $file->getTokensAsString($commentStart, $commentEnd - $commentStart + 1);
0 ignored issues
show
Bug introduced by
It seems like $commentStart defined by $file->findPrevious(T_DO...PEN_TAG, $position - 1) on line 43 can also be of type boolean; however, PHP_CodeSniffer_File::getTokensAsString() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
	}
47
48
}
49