regex_match.php ➔ RegexMatch()   B
last analyzed

Complexity

Conditions 10
Paths 5

Size

Total Lines 26

Duplication

Lines 21
Ratio 80.77 %

Importance

Changes 0
Metric Value
cc 10
nc 5
nop 3
dl 21
loc 26
rs 7.6666
c 0
b 0
f 0

How to fix   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
 * @package		fwolflib
4
 * @copyright	Copyright 2007-2010, Fwolf
5
 * @author		Fwolf <[email protected]>
6
 * @since		2007-08-15
7
 */
8
9
10
require_once(dirname(__FILE__) . '/../fwolflib.php');
11
12
13
/**
14
 * Match content using preg, return result array or '' if non-match
15
 * To read content currectly, content parsing is nesessary
16
 * Return value maybe string or array, use careful and
17
 *   remind which value you use it for.
18
 *
19
 * @deprecated      Use Fwlib\Util\StringUtil::matchRegex()
20
 * @param	string	$preg
21
 * @param	string	$str
22
 * @param 	boolean	$csrts	Convert single result to str(array -> str) ?
23
 * @return	mixed
24
 * @access	public
25
 */
26
function RegexMatch($preg, $str = '', $csrts = true) {
27
	if (empty($preg) || empty($str)) return '';
28
	$i = preg_match_all($preg, $str, $ar, PREG_SET_ORDER);
29 View Code Duplication
	if (0 == $i || false === $i)
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...
30
		// Got none match or Got error
31
		$ar = '';
32
	elseif (1 == $i)
33
	{
34
		// Got 1 match, return as string or array(2 value in 1 match)
35
		$ar = $ar[0];
36
		array_shift($ar);
37
		if (1 == count($ar) && true == $csrts)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
38
			$ar = $ar[0];
39
	}
40
	else
41
	{
42
		// Got more than 1 match return array contains string or sub-array
43
		foreach ($ar as &$row)
0 ignored issues
show
Bug introduced by
The expression $ar 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...
44
		{
45
			array_shift($row);
46
			if (1 == count($row))
47
				$row = $row[0];
48
		}
49
	}
50
	return $ar;
51
} // end of func RegexMatch
52
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
53