Conditions | 11 |
Paths | 16 |
Total Lines | 33 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
29 | protected function checkType($expectedType, $value, $nullAllowed = false) |
||
30 | { |
||
31 | $invalidType = null; |
||
32 | $valueType = gettype($value); |
||
33 | $nullAllowed = (boolean) $nullAllowed; |
||
34 | |||
35 | if ($valueType === 'object') { |
||
36 | if (!is_a($value, $expectedType)) { |
||
37 | $invalidType = get_class($value); |
||
38 | } |
||
39 | } elseif ($expectedType !== $valueType) { |
||
40 | $invalidType = $valueType; |
||
41 | } |
||
42 | |||
43 | if (PHP_INT_SIZE == 4 && $invalidType == "double" && $expectedType == "integer") |
||
44 | { |
||
45 | //TODO: 32bit - handle this specially? |
||
46 | $invalidType = null; |
||
47 | } |
||
48 | |||
49 | if ($invalidType !== null && ($nullAllowed === false || ($nullAllowed === true && $value !== null))) { |
||
50 | throw new CmisInvalidArgumentException( |
||
51 | sprintf( |
||
52 | 'Argument of type "%s" given but argument of type "%s" was expected.', |
||
53 | $invalidType, |
||
54 | $expectedType |
||
55 | ), |
||
56 | 1413440336 |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | return true; |
||
61 | } |
||
62 | |||
96 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.