Completed
Push — master ( a32d1b...3011b4 )
by Filip
8s
created

Helpers::getPropertyLine()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 31
rs 4.8196
cc 10
eloc 18
nc 16
nop 1

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
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Doctrine;
12
13
use Kdyby;
14
use Nette;
15
use Doctrine\DBAL\Types\Type;
16
17
18
19
/**
20
 * @author Filip Procházka <[email protected]>
21
 */
22
class Helpers extends Nette\Object
23
{
24
25
26
	/**
27
	 * @param QueryBuilder|NativeQueryBuilder $query
28
	 * @param array $args
29
	 * @return array
30
	 */
31
	public static function separateParameters($query, array $args)
32
	{
33
		for ($i = 0; array_key_exists($i, $args) && array_key_exists($i + 1, $args) && ($arg = $args[$i]); $i++) {
34
			if ( ! preg_match_all('~((\\:|\\?)(?P<name>[a-z0-9_]+))(?=(?:\\z|\\s|\\)))~i', $arg, $m)) {
35
				continue;
36
			}
37
38
			$repeatedArgs = [];
39
			foreach ($m['name'] as $l => $name) {
40
				if (isset($repeatedArgs[$name])) {
41
					continue;
42
				}
43
44
				$value = $args[++$i];
45
				$type = NULL;
46
47
				if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
0 ignored issues
show
Bug introduced by
The class DateTimeImmutable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
48
					$type = Type::DATETIME;
49
50
				} elseif (is_array($value)) {
51
					$type = Connection::PARAM_STR_ARRAY;
52
				}
53
54
				$query->setParameter($name, $value, $type);
55
				$repeatedArgs[$name] = TRUE;
56
				unset($args[$i]);
57
			}
58
		}
59
60
		return $args;
61
	}
62
63
64
65
	/**
66
	 * @param \ReflectionProperty $property
67
	 * @return int
68
	 */
69
	public static function getPropertyLine(\ReflectionProperty $property)
70
	{
71
		$class = $property->getDeclaringClass();
72
73
		$context = 'file';
74
		$contextBrackets = 0;
75
		foreach (token_get_all(file_get_contents($class->getFileName())) as $token) {
76
			if ($token === '{') {
77
				$contextBrackets += 1;
78
79
			} elseif ($token === '}') {
80
				$contextBrackets -= 1;
81
			}
82
83
			if (!is_array($token)) {
84
				continue;
85
			}
86
87
			if ($token[0] === T_CLASS) {
88
				$context = 'class';
89
				$contextBrackets = 0;
90
91
			} elseif ($context === 'class' && $contextBrackets === 1 && $token[0] === T_VARIABLE) {
92
				if ($token[1] === '$' . $property->getName()) {
93
					return $token[2];
94
				}
95
			}
96
		}
97
98
		return NULL;
99
	}
100
101
102
103
	/**
104
	 * @param array $one
105
	 * @param array $two
106
	 *
107
	 * @return array
108
	 */
109
	public static function zipper(array $one, array $two)
110
	{
111
		$output = [];
112
		while ($one && $two) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $one of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $two of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
113
			$output[] = array_shift($one);
114
			$output[] = array_shift($two);
115
		}
116
117
		return array_merge($output, $one ? : [], $two ? : []);
118
	}
119
120
}
121