SQL::toSource()   B
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 30
rs 7.3166
cc 11
nc 10
nop 4

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
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2025
7
 * @package Base
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\Base\Criteria\Expression\Combine;
13
14
15
/**
16
 * SQL implementation for combining objects.
17
 *
18
 * @package Base
19
 * @subpackage Common
20
 */
21
class SQL extends Base
22
{
23
	private static $operators = array( '&&' => 'AND', '||' => 'OR', '!' => 'NOT' );
24
25
26
	/**
27
	 * Initializes the object.
28
	 *
29
	 * @param string $operator The used combine operator
30
	 * @param array $list List of expression objects
31
	 */
32
	public function __construct( string $operator, array $list )
33
	{
34
		if( !isset( self::$operators[$operator] ) ) {
35
			throw new \Aimeos\Base\Exception( sprintf( 'Invalid operator "%1$s"', $operator ) );
36
		}
37
38
		parent::__construct( $operator, $list );
39
	}
40
41
42
	/**
43
	 * Returns the available operators for the expression.
44
	 *
45
	 * @return array List of available operators
46
	 */
47
	public static function getOperators() : array
48
	{
49
		return array_keys( self::$operators );
50
	}
51
52
53
	/**
54
	 * Generates a string from the expression objects.
55
	 *
56
	 * @param array $types Associative list of variable or column names as keys and their corresponding types
57
	 * @param array $translations Associative list of variable or column names that should be translated
58
	 * @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of item names as keys and plugins objects as values
59
	 * @param array $funcs Associative list of item names and functions modifying the conditions
60
	 * @return mixed Expression that evaluates to a boolean result
61
	 */
62
	public function toSource( array $types, array $translations = [], array $plugins = [], array $funcs = [] )
63
	{
64
		$expr = $this->getExpressions();
65
66
		if( ( $item = reset( $expr ) ) === false ) {
67
			return '';
68
		}
69
70
		$op = $this->getOperator();
71
		$string = $item->toSource( $types, $translations, $plugins, $funcs );
72
73
		if( $op == '!' && $string !== '' && $string !== null ) {
74
			return ' ' . self::$operators[$op] . ' ( ' . $string . ' )';
75
		}
76
77
		while( ( $item = next( $expr ) ) !== false )
78
		{
79
			$itemstr = $item->toSource( $types, $translations, $plugins, $funcs );
80
81
			if( $itemstr !== '' && $itemstr !== null )
82
			{
83
				if( $string !== '' && $string !== null ) {
84
					$string .= ' ' . self::$operators[$op] . ' ' . $itemstr;
85
				} else {
86
					$string = $itemstr;
87
				}
88
			}
89
		}
90
91
		return $string ? '( ' . $string . ' )' : '';
92
	}
93
94
95
	/**
96
	 * Translates the sort key into the name required by the storage
97
	 *
98
	 * @param array $translations Associative list of variable or column names that should be translated
99
	 * @param array $funcs Associative list of item names and functions modifying the conditions
100
	 * @return string|null Translated name (with replaced parameters if the name is an expression function)
101
	 */
102
	public function translate( array $translations, array $funcs = [] ) : ?string
103
	{
104
		return null;
105
	}
106
}
107