Conditions | 5 |
Paths | 16 |
Total Lines | 76 |
Code Lines | 58 |
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 |
||
47 | public function regenerateUsersOrders(): void |
||
48 | { |
||
49 | $connection = $this->getConnection(false); |
||
50 | $schemaManager = $connection->getSchemaManager(); |
||
51 | |||
52 | try { |
||
53 | $schemaManager->dropDatabase($GLOBALS['db_name']); |
||
54 | } catch (\Exception $e) { |
||
55 | // If tardet database doesn't exist. |
||
56 | } |
||
57 | |||
58 | $schemaManager->createDatabase($GLOBALS['db_name']); |
||
59 | $connection->close(); |
||
60 | |||
61 | $connection = $this->getConnection(); |
||
62 | $schemaManager = $connection->getSchemaManager(); |
||
63 | $schema = $schemaManager->createSchema(); |
||
64 | |||
65 | $user = $schema->createTable('users'); |
||
66 | $user->addColumn('id', 'integer', ['id' => true, 'unsigned' => true, 'unique']); |
||
67 | $user->addColumn('email', 'string', ['length' => 256, 'notnull' => false]); |
||
68 | $user->addColumn('firstname', 'string', ['length' => 256, 'notnull' => false]); |
||
69 | $user->addColumn('lastname', 'string', ['length' => 256, 'notnull' => false]); |
||
70 | $user->addColumn('birthdate', 'date', ['notnull' => false]); |
||
71 | $user->addColumn('phone', 'string', ['length' => 20, 'notnull' => false]); |
||
72 | $user->addColumn('password', 'string', ['length' => 64, 'notnull' => false]); |
||
73 | $user->setPrimaryKey(['id']); |
||
74 | $schemaManager->createTable($user); |
||
75 | |||
76 | $order = $schema->createTable('orders'); |
||
77 | $order->addColumn('id', 'integer', ['unsigned' => true]); |
||
78 | $order->addColumn('address', 'string', ['length' => 256, 'notnull' => false]); |
||
79 | $order->addColumn('street_address', 'string', ['length' => 64, 'notnull' => false]); |
||
80 | $order->addColumn('zip_code', 'string', ['length' => 10, 'notnull' => false]); |
||
81 | $order->addColumn('city', 'string', ['length' => 64, 'notnull' => false]); |
||
82 | $order->addColumn('country', 'string', ['length' => 64, 'notnull' => false]); |
||
83 | $order->addColumn('comment', 'text', ['notnull' => false]); |
||
84 | $order->addColumn('created_at', 'datetime', ['notnull' => false]); |
||
85 | $order->addColumn('user_id', 'integer', ['unsigned' => true, 'notnull' => false]); |
||
86 | $order->setPrimaryKey(['id']); |
||
87 | $order->addForeignKeyConstraint($user, ['user_id'], ['id']); |
||
88 | $schemaManager->createTable($order); |
||
89 | |||
90 | $productivity = $schema->createTable('productivity'); |
||
91 | $productivity->addColumn('day', 'date', ['notnull' => false]); |
||
92 | $productivity->addColumn('user_id', 'integer', ['unsigned' => true, 'notnull' => false]); |
||
93 | $productivity->addColumn('feedback', 'text', ['notnull' => false]); |
||
94 | $productivity->setPrimaryKey(['day', 'user_id']); |
||
95 | $productivity->addForeignKeyConstraint($user, ['user_id'], ['id']); |
||
96 | $schemaManager->createTable($productivity); |
||
97 | |||
98 | foreach (range(1, 10) as $i) { |
||
99 | $connection->createQueryBuilder() |
||
100 | ->insert('users') |
||
101 | ->values(['id' => $i]) |
||
102 | ->execute(); |
||
103 | } |
||
104 | |||
105 | foreach (range(1, 20) as $i) { |
||
|
|||
106 | $connection->createQueryBuilder() |
||
107 | ->insert('orders') |
||
108 | ->values(['id' => $i, 'user_id' => mt_rand(1, 10)]) |
||
109 | ->execute(); |
||
110 | } |
||
111 | |||
112 | foreach (range(1, 30) as $i) { |
||
113 | $connection->createQueryBuilder() |
||
114 | ->insert('productivity') |
||
115 | ->values([ |
||
116 | 'day' => $connection->quote(new \DateTime("+$i days"), 'date'), |
||
117 | 'user_id' => mt_rand(1, 10) |
||
118 | ]) |
||
119 | ->execute(); |
||
120 | } |
||
121 | |||
122 | $connection->close(); |
||
123 | } |
||
125 |