PhpDoc::getPhpDocOfMethod()   C
last analyzed

Complexity

Conditions 11
Paths 28

Size

Total Lines 66
Code Lines 28

Duplication

Lines 8
Ratio 12.12 %

Importance

Changes 0
Metric Value
dl 8
loc 66
rs 5.9447
c 0
b 0
f 0
cc 11
eloc 28
nc 28
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Manage php doc
5
 *
6
 * @category  	lib
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\lib;
16
17
/**
18
 * This class manage the php doc
19
 *
20
 * @category  	lib
21
 * @author    	Judicaël Paquet <[email protected]>
22
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
23
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
24
 * @version   	Release: 1.0.0
25
 * @filesource	https://github.com/las93/venus2
26
 * @link      	https://github.com/las93
27
 * @since     	1.0
28
 */
29
class PhpDoc
30
{
31
	/**
32
	 * add the namespace
33
	 *
34
	 * @access public
35
	 * @param mixed $sClassName class name
36
	 * @param string $sMethodName method name
37
	 * @return array
38
	 */
39
	public static function getPhpDocOfMethod($sClassName, string $sMethodName) : array
40
	{
41
		$oReflectionClass  = new \ReflectionClass($sClassName);
42
		$oReflectionMethod = $oReflectionClass->getMethod($sMethodName);
43
		$sCommentPhpDoc = $oReflectionMethod->getDocComment();
44
45
		/**
46
		 * parse this PhpDoc @cache(param=1,param=2)
47
		 */
48
49
		preg_match_all('/@([a-zA-Z0-9_-]+)[ \t]*\(([^\)]+)\)/', $sCommentPhpDoc, $aMatchs, PREG_SET_ORDER);
50
		$aParams = array();
51
52
		foreach ($aMatchs as $aOneMatch) {
0 ignored issues
show
Bug introduced by
The expression $aMatchs of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53
54
			$aParams[$aOneMatch[1]] = array();
55
56
			foreach (explode(',', $aOneMatch[2]) as $sValue) {
57
58
				$aFinalExplode = explode('=', $sValue);
59
60
				if (preg_match('/^".+"$/', $aFinalExplode[1]) || preg_match("/^'.+'$/", $aFinalExplode[1])) {
61
62
					$aFinalExplode[1] = substr($aFinalExplode[1], 1);
63
					$aFinalExplode[1] = substr($aFinalExplode[1], 0, -1);
64
				}
65
66
				$aParams[$aOneMatch[1]][$aFinalExplode[0]] = $aFinalExplode[1];
67
			}
68
		}
69
70
		/**
71
		 * parse this PhpDoc @param  string $title ...
72
		 */
73
74
		preg_match_all('/@([a-zA-Z0-9_-]+)[ \t]*([^\r\n]+)/', $sCommentPhpDoc, $aMatchs, PREG_SET_ORDER);
75
		$aParams = array();
76
77
		foreach ($aMatchs as $aOneMatch) {
0 ignored issues
show
Bug introduced by
The expression $aMatchs of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
78
79
			if ($aOneMatch[1] == 'param') {
80
81
				if (!isset($aParams['param'])) { $aParams['param'] = array(); }
82
83
				$iIndex = count($aParams['param']);
84
85
				if (!isset($aParams['param'][$iIndex])) { $aParams['param'][$iIndex] = array(); }
86
87 View Code Duplication
				foreach (explode(' ', $aOneMatch[2]) as $sValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
89
					$aParams['param'][$iIndex][] = $sValue;
90
				}
91
			}
92
			else {
93
94
				$aParams[$aOneMatch[1]] = array();
95
96 View Code Duplication
				foreach (explode(' ', $aOneMatch[2]) as $sValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
98
					$aParams[$aOneMatch[1]][] = $sValue;
99
				}
100
			}
101
		}
102
103
		return $aParams;
104
	}
105
}
106