Conditions | 7 |
Paths | 5 |
Total Lines | 56 |
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 |
||
18 | public function add($f) |
||
19 | { |
||
20 | $userInfo = self::$auth->getUser(); |
||
21 | $f['added'] = date('Y-m-d H:i:s'); |
||
22 | $f['adder_ID'] = $userInfo['id']; |
||
23 | |||
24 | $this->pdo->beginTransaction(); |
||
25 | |||
26 | $stmt = $this->pdo->prepare(" |
||
27 | INSERT INTO {$this->prefix}galleries |
||
28 | (title |
||
29 | , added |
||
30 | , adder_ID |
||
31 | , slug |
||
32 | , thumb_width |
||
33 | , thumb_height) |
||
34 | VALUES |
||
35 | (:title |
||
36 | , :added |
||
37 | , :adder_ID |
||
38 | , :slug |
||
39 | , :thumb_width |
||
40 | , :thumb_height) |
||
41 | "); |
||
42 | $stmt->bindValue(':title', $f['title'], \PDO::PARAM_STR); |
||
43 | $stmt->bindValue(':added', $f['added'], \PDO::PARAM_STR); |
||
44 | $stmt->bindValue(':adder_ID', $f['adder_ID'], \PDO::PARAM_INT); |
||
45 | $stmt->bindValue(':slug', $f['slug'], \PDO::PARAM_STR); |
||
46 | $stmt->bindValue(':thumb_width', $f['thumb_width'] ? $f['thumb_width'] : 209, \PDO::PARAM_INT); |
||
47 | $stmt->bindValue(':thumb_height', $f['thumb_height'] ? $f['thumb_height'] : 157, \PDO::PARAM_INT); |
||
48 | |||
49 | if (!$stmt->execute()) { |
||
50 | return false; |
||
51 | } |
||
52 | |||
53 | $id = $this->pdo->lastInsertId(); |
||
54 | $config = (new Module('galleries'))->getConfig(); |
||
55 | $directory = $config['path_root'].'/'.$f['slug']; |
||
56 | |||
57 | if (file_exists($directory)) { |
||
58 | $directory = $directory.'-'.$id; |
||
59 | $stmt = $this->pdo->prepare("UPDATE {$this->prefix}galleries SET slug = :slug WHERE id = :id"); |
||
60 | $stmt->bindValue(':slug', $f['slug'].'-'.$id, \PDO::PARAM_STR); |
||
61 | $stmt->bindValue(':id', $id, \PDO::PARAM_INT); |
||
62 | $stmt->execute(); |
||
63 | } |
||
64 | |||
65 | if (!mkdir($directory, 0777, true) && !is_dir($directory)) { |
||
66 | $this->pdo->rollBack(); |
||
67 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory)); |
||
68 | } |
||
69 | |||
70 | $this->pdo->commit(); |
||
71 | |||
72 | return $id; |
||
73 | } |
||
74 | } |
||
75 |