Conditions | 4 |
Paths | 8 |
Total Lines | 53 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
41 | public function create($nameId, $tokenType, $institution, bool $selfAsserted = false, $identifier = null) |
||
42 | { |
||
43 | $uuid = Uuid::uuid4()->toString(); |
||
44 | |||
45 | // If an identifier is not important, simply use the UUID, otherwise use the provide one |
||
46 | if (!$identifier) { |
||
47 | $identifier = $uuid; |
||
48 | } |
||
49 | |||
50 | $data = [ |
||
51 | 'identityId' => $uuid, |
||
52 | 'nameId' => $nameId, |
||
53 | 'institution' => $institution, |
||
54 | 'secondFactorId' => $uuid, |
||
55 | 'secondFactorType' => $tokenType, |
||
56 | 'secondFactorIdentifier' => $identifier, |
||
57 | 'id' => $uuid, |
||
58 | 'displayLocale' => 'en_GB', |
||
59 | 'identityVetted' => $selfAsserted ? 0 : 1, |
||
60 | ]; |
||
61 | $sql = <<<SQL |
||
62 | INSERT INTO second_factor ( |
||
63 | identity_id, |
||
64 | name_id, |
||
65 | institution, |
||
66 | second_factor_id, |
||
67 | second_factor_type, |
||
68 | second_factor_identifier, |
||
69 | id, |
||
70 | display_locale, |
||
71 | identity_vetted |
||
72 | ) |
||
73 | VALUES ( |
||
74 | :identityId, |
||
75 | :nameId, |
||
76 | :institution, |
||
77 | :secondFactorId, |
||
78 | :secondFactorType, |
||
79 | :secondFactorIdentifier, |
||
80 | :id, |
||
81 | :displayLocale, |
||
82 | :identityVetted |
||
83 | ) |
||
84 | SQL; |
||
85 | $stmt = $this->connection->prepare($sql); |
||
86 | if ($stmt->execute($data)) { |
||
87 | return $data; |
||
88 | } |
||
89 | |||
90 | throw new Exception( |
||
91 | sprintf( |
||
92 | 'Unable to insert the new second_factor. PDO raised this error: "%s"', |
||
93 | $stmt->errorInfo()[2] |
||
94 | ) |
||
122 |