Helpers   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 26
lcom 0
cbo 0
dl 0
loc 104
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B separateParameters() 0 31 10
B getPropertyLine() 0 31 10
A zipper() 0 10 5
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
final class Helpers
23
{
24
25
	private function __construct()
26
	{
27
	}
28
29
30
31
	/**
32
	 * @param QueryBuilder|NativeQueryBuilder $query
33
	 * @param array $args
34
	 * @return array
35
	 */
36
	public static function separateParameters($query, array $args)
37
	{
38
		for ($i = 0; array_key_exists($i, $args) && array_key_exists($i + 1, $args) && ($arg = $args[$i]); $i++) {
39
			if ( ! preg_match_all('~((\\:|\\?)(?P<name>[a-z0-9_]+))(?=(?:\\z|\\s|\\)))~i', $arg, $m)) {
40
				continue;
41
			}
42
43
			$repeatedArgs = [];
44
			foreach ($m['name'] as $l => $name) {
45
				if (isset($repeatedArgs[$name])) {
46
					continue;
47
				}
48
49
				$value = $args[++$i];
50
				$type = NULL;
51
52
				if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
53
					$type = Type::DATETIME;
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DATETIME has been deprecated with message: Use {@see Types::DATETIME_MUTABLE} instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
54
55
				} elseif (is_array($value)) {
56
					$type = Connection::PARAM_STR_ARRAY;
57
				}
58
59
				$query->setParameter($name, $value, $type);
60
				$repeatedArgs[$name] = TRUE;
61
				unset($args[$i]);
62
			}
63
		}
64
65
		return $args;
66
	}
67
68
69
70
	/**
71
	 * @param \ReflectionProperty $property
72
	 * @return int|NULL
73
	 */
74
	public static function getPropertyLine(\ReflectionProperty $property)
75
	{
76
		$class = $property->getDeclaringClass();
77
78
		$context = 'file';
79
		$contextBrackets = 0;
80
		foreach (token_get_all(file_get_contents($class->getFileName())) as $token) {
81
			if ($token === '{') {
82
				$contextBrackets += 1;
83
84
			} elseif ($token === '}') {
85
				$contextBrackets -= 1;
86
			}
87
88
			if (!is_array($token)) {
89
				continue;
90
			}
91
92
			if ($token[0] === T_CLASS) {
93
				$context = 'class';
94
				$contextBrackets = 0;
95
96
			} elseif ($context === 'class' && $contextBrackets === 1 && $token[0] === T_VARIABLE) {
97
				if ($token[1] === '$' . $property->getName()) {
98
					return $token[2];
99
				}
100
			}
101
		}
102
103
		return NULL;
104
	}
105
106
107
108
	/**
109
	 * @param array $one
110
	 * @param array $two
111
	 *
112
	 * @return array
113
	 */
114
	public static function zipper(array $one, array $two)
115
	{
116
		$output = [];
117
		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...
118
			$output[] = array_shift($one);
119
			$output[] = array_shift($two);
120
		}
121
122
		return array_merge($output, $one ? : [], $two ? : []);
123
	}
124
125
}
126