SimpleParameterFormatter::format()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 6.9666
c 0
b 0
f 0
cc 12
nc 11
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\Diagnostics;
12
13
use Kdyby;
14
use Nette;
15
16
17
18
/**
19
 * @author Martin Štekl <[email protected]>
20
 */
21
class SimpleParameterFormatter
22
{
23
24
	use \Kdyby\StrictObjects\Scream;
25
26
	/**
27
	 * @param mixed $param
28
	 * @return mixed
29
	 */
30
	public static function format($param)
31
	{
32
		if (is_int($param) || is_float($param)) {
33
			return $param;
34
35
		} elseif (is_string($param)) {
36
			return "'" . addslashes($param) . "'";
37
38
		} elseif (is_null($param)) {
39
			return "NULL";
40
41
		} elseif (is_bool($param)) {
42
			return $param ? "TRUE" : "FALSE";
43
44
		} elseif (is_array($param)) {
45
			$formatted = [];
46
			foreach ($param as $value) {
47
				$formatted[] = self::format($value);
48
			}
49
			return implode(', ', $formatted);
50
51
		} elseif ($param instanceof \DateTime) {
52
			return "'" . $param->format('Y-m-d H:i:s') . "'";
53
54
		} elseif (is_object($param)) {
55
			return get_class($param) . (method_exists($param, 'getId') ? '(' . $param->getId() . ')' : '');
56
57
		} else {
58
			return @"'$param'";
59
		}
60
	}
61
62
}
63