Conditions | 7 |
Paths | 18 |
Total Lines | 53 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
37 | public function insertEx(string $table, array $columns): bool|array |
||
38 | { |
||
39 | $params = []; |
||
40 | $sql = $this->queryBuilder()->insertEx($table, $columns, $params); |
||
41 | |||
42 | $tableSchema = $this->db->getSchema()->getTableSchema($table); |
||
43 | |||
44 | $returnColumns = $tableSchema?->getPrimaryKey() ?? []; |
||
45 | $columnSchemas = $tableSchema?->getColumns() ?? []; |
||
46 | |||
47 | $returnParams = []; |
||
48 | $returning = []; |
||
49 | foreach ($returnColumns as $name) { |
||
50 | $phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams)); |
||
51 | |||
52 | $returnParams[$phName] = [ |
||
53 | 'column' => $name, |
||
54 | 'value' => '', |
||
55 | ]; |
||
56 | |||
57 | if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== Schema::PHP_TYPE_INTEGER) { |
||
58 | $returnParams[$phName]['dataType'] = PDO::PARAM_STR; |
||
59 | } else { |
||
60 | $returnParams[$phName]['dataType'] = PDO::PARAM_INT; |
||
61 | } |
||
62 | |||
63 | $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1; |
||
64 | |||
65 | $returning[] = $this->db->getQuoter()->quoteColumnName($name); |
||
66 | } |
||
67 | |||
68 | $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams)); |
||
69 | |||
70 | $this->setSql($sql)->bindValues($params); |
||
71 | $this->prepare(false); |
||
72 | |||
73 | /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */ |
||
74 | foreach ($returnParams as $name => &$value) { |
||
75 | $this->bindParam($name, $value['value'], $value['dataType'], $value['size']); |
||
76 | } |
||
77 | |||
78 | if (!$this->execute()) { |
||
79 | return false; |
||
80 | } |
||
81 | |||
82 | $result = []; |
||
83 | |||
84 | foreach ($returnParams as $value) { |
||
85 | /** @var mixed */ |
||
86 | $result[$value['column']] = $value['value']; |
||
87 | } |
||
88 | |||
89 | return $result; |
||
90 | } |
||
142 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: