| Conditions | 3 |
| Paths | 3 |
| Total Lines | 51 |
| Lines | 11 |
| Ratio | 21.57 % |
| 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 |
||
| 84 | public function createIdpIfNotExists($entityId, $certificate) |
||
| 85 | { |
||
| 86 | // Does the SP exist? |
||
| 87 | $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1'); |
||
| 88 | $stmt->bindParam('entityId', $entityId, PDO::PARAM_STR); |
||
| 89 | $stmt->execute(); |
||
| 90 | if ($stmt->rowCount() === 0) { |
||
| 91 | // If not, create it |
||
| 92 | $uuid = Uuid::uuid4()->toString(); |
||
| 93 | $type = 'idp'; |
||
| 94 | |||
| 95 | $configuration['public_key'] = $certificate; |
||
| 96 | |||
| 97 | $data = [ |
||
| 98 | 'entityId' => $entityId, |
||
| 99 | 'type' => $type, |
||
| 100 | 'configuration' => json_encode($configuration), |
||
| 101 | 'id' => $uuid, |
||
| 102 | ]; |
||
| 103 | $sql = <<<SQL |
||
| 104 | INSERT INTO saml_entity ( |
||
| 105 | `entity_id`, |
||
| 106 | `type`, |
||
| 107 | `configuration`, |
||
| 108 | `id` |
||
| 109 | ) |
||
| 110 | VALUES ( |
||
| 111 | :entityId, |
||
| 112 | :type, |
||
| 113 | :configuration, |
||
| 114 | :id |
||
| 115 | ) |
||
| 116 | SQL; |
||
| 117 | $stmt = $this->connection->prepare($sql); |
||
| 118 | if ($stmt->execute($data)) { |
||
| 119 | return $data; |
||
| 120 | } |
||
| 121 | |||
| 122 | throw new Exception('Unable to insert the new SP saml_entity'); |
||
| 123 | View Code Duplication | } else { |
|
| 124 | // Return the SP data |
||
| 125 | $results = reset($stmt->fetchAll()); |
||
| 126 | $data = [ |
||
| 127 | 'entityId' => $results['entity_id'], |
||
| 128 | 'type' => $results['type'], |
||
| 129 | 'configuration' => $results['configuration'], |
||
| 130 | 'id' => $results['id'], |
||
| 131 | ]; |
||
| 132 | return $data; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.