1
|
|
|
<?php |
2
|
|
|
namespace Dkd\PhpCmis\Traits; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of php-cmis-lib. |
6
|
|
|
* |
7
|
|
|
* (c) Sascha Egerer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use Dkd\PhpCmis\Exception\CmisInvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait with some type check related functions |
17
|
|
|
*/ |
18
|
|
|
trait TypeHelperTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Check if the given value is the expected object type |
22
|
|
|
* |
23
|
|
|
* @param string $expectedType the expected object type (class name) |
24
|
|
|
* @param mixed $value The value that has to be checked |
25
|
|
|
* @param boolean $nullAllowed Is <code>null</code> allowed as value? |
26
|
|
|
* @return boolean returns <code>true</code> if the given value is instance of expected type |
27
|
|
|
* @throws CmisInvalidArgumentException Exception is thrown if the given value does not match to the expected type |
28
|
|
|
*/ |
29
|
825 |
|
protected function checkType($expectedType, $value, $nullAllowed = false) |
30
|
|
|
{ |
31
|
825 |
|
$invalidType = null; |
32
|
825 |
|
$valueType = gettype($value); |
33
|
825 |
|
$nullAllowed = (boolean) $nullAllowed; |
34
|
|
|
|
35
|
825 |
|
if ($valueType === 'object') { |
36
|
69 |
|
if (!is_a($value, $expectedType)) { |
37
|
11 |
|
$invalidType = get_class($value); |
38
|
11 |
|
} |
39
|
825 |
|
} elseif ($expectedType !== $valueType) { |
40
|
435 |
|
$invalidType = $valueType; |
41
|
435 |
|
} |
42
|
|
|
|
43
|
825 |
|
if ($invalidType !== null && ($nullAllowed === false || ($nullAllowed === true && $value !== null))) { |
44
|
394 |
|
throw new CmisInvalidArgumentException( |
45
|
394 |
|
sprintf( |
46
|
394 |
|
'Argument of type "%s" given but argument of type "%s" was expected.', |
47
|
394 |
|
$invalidType, |
48
|
|
|
$expectedType |
49
|
394 |
|
), |
50
|
|
|
1413440336 |
51
|
394 |
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
734 |
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Ensure that a value is an instance of the expected type. If not the value |
59
|
|
|
* is casted to the expected type and a log message is triggered. |
60
|
|
|
* |
61
|
|
|
* @param string $expectedType the expected object type (class name) |
62
|
|
|
* @param mixed $value The value that has to be checked |
63
|
|
|
* @param boolean $nullIsValidValue defines if <code>null</code> is also a valid value |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
613 |
|
protected function castValueToSimpleType($expectedType, $value, $nullIsValidValue = false) |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
613 |
|
$this->checkType($expectedType, $value, $nullIsValidValue); |
70
|
613 |
|
} catch (CmisInvalidArgumentException $exception) { |
71
|
316 |
|
if (PHP_INT_SIZE == 4 && $expectedType == "integer" && gettype($value) == "double") { |
|
|
|
|
72
|
|
|
//32bit - handle this specially? |
73
|
|
|
} else { |
74
|
316 |
|
trigger_error( |
75
|
316 |
|
sprintf( |
76
|
|
|
'Given value is of type "%s" but a value of type "%s" was expected.' |
77
|
316 |
|
. ' Value has been casted to the expected type.', |
78
|
316 |
|
gettype($value), |
79
|
|
|
$expectedType |
80
|
316 |
|
), |
81
|
|
|
E_USER_NOTICE |
82
|
316 |
|
); |
83
|
295 |
|
settype($value, $expectedType); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
592 |
|
return $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.