Passed
Push — master ( 6a58b9...6d010e )
by Aimeos
02:10
created

SQL::escape()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 26
rs 7.6666
cc 10
nc 10
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A SQL::getParamType() 0 11 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-2023
7
 * @package Base
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\Base\Criteria\Expression\Sort;
13
14
15
/**
16
 * SQL implementation for sorting objects.
17
 *
18
 * @package Base
19
 * @subpackage Common
20
 */
21
class SQL extends Base
22
{
23
	private static $operators = array( '+' => 'ASC', '-' => 'DESC' );
24
	private \Aimeos\Base\DB\Connection\Iface $conn;
25
26
27
	/**
28
	 * Initializes the object.
29
	 *
30
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection object
31
	 * @param string $operator Sorting operator ("+": ascending, "-": descending)
32
	 * @param string $name Name of the variable or column to sort
33
	 */
34
	public function __construct( \Aimeos\Base\DB\Connection\Iface $conn, string $operator, string $name )
35
	{
36
		if( !isset( self::$operators[$operator] ) ) {
37
			throw new \Aimeos\Base\Exception( sprintf( 'Invalid operator "%1$s"', $operator ) );
38
		}
39
40
		parent::__construct( $operator, $name );
41
		$this->conn = $conn;
42
	}
43
44
45
	/**
46
	 * Returns the available operators for the expression.
47
	 *
48
	 * @return array List of available operators
49
	 */
50
	public static function getOperators() : array
51
	{
52
		return array_keys( self::$operators );
53
	}
54
55
56
	/**
57
	 * Generates a string from the expression objects.
58
	 *
59
	 * @param array $types Associative list of variable or column names as keys and their corresponding types
60
	 * @param array $translations Associative list of variable or column names that should be translated
61
	 * @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of item names as keys and plugin objects as values
62
	 * @param array $funcs Associative list of item names and functions modifying the conditions
63
	 * @return mixed Expression that evaluates to a boolean result
64
	 */
65
	public function toSource( array $types, array $translations = [], array $plugins = [], array $funcs = [] )
66
	{
67
		$this->setPlugins( $plugins );
68
69
		$name = $this->getName();
70
		$transname = $this->translateName( $name, $translations, $funcs );
71
72
		if( !$transname ) {
73
			throw new \Aimeos\Base\Exception( sprintf( 'Invalid sorting "%1$s"', $this->getName() ) );
74
		}
75
76
		if( !isset( $types[$name] ) ) {
77
			throw new \Aimeos\Base\Exception( sprintf( 'Invalid name "%1$s"', $name ) );
78
		}
79
80
		return $transname . ' ' . self::$operators[$this->getOperator()];
0 ignored issues
show
Bug introduced by
Are you sure $transname of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
		return /** @scrutinizer ignore-type */ $transname . ' ' . self::$operators[$this->getOperator()];
Loading history...
81
	}
82
83
84
	/**
85
	 * Returns the connection object.
86
	 *
87
	 * return \Aimeos\Base\DB\Connection\Iface Connection object
88
	 */
89
	public function getConnection() : \Aimeos\Base\DB\Connection\Iface
90
	{
91
		return $this->conn;
92
	}
93
94
95
	/**
96
	 * Returns the internal type of the function parameter.
97
	 *
98
	 * @param mixed &$item Reference to parameter value (will be updated if necessary)
99
	 * @return string Internal parameter type
100
	 * @throws \Aimeos\Base\Exception If an error occurs
101
	 */
102
	protected function getParamType( &$item ) : string
103
	{
104
		if( is_null( $item ) ) {
105
			return \Aimeos\Base\DB\Statement\Base::PARAM_NULL;
106
		} elseif( is_float( $item ) ) {
107
			return \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT;
108
		} elseif( is_int( $item ) ) {
109
			return \Aimeos\Base\DB\Statement\Base::PARAM_INT;
110
		}
111
112
		return \Aimeos\Base\DB\Statement\Base::PARAM_STR;
113
	}
114
}
115