PhpDoc   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 10.39 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 8
loc 77
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getPhpDocOfMethod() 8 66 11

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
/**
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