| Conditions | 7 |
| Paths | 16 |
| Total Lines | 53 |
| 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 |
||
| 32 | public function up(Schema $schema): void |
||
| 33 | { |
||
| 34 | $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.'); |
||
| 35 | |||
| 36 | $entityManager = $this->container->get('doctrine.orm.default_entity_manager'); |
||
| 37 | |||
| 38 | $rsm = new ResultSetMapping(); |
||
| 39 | $rsm->addScalarResult('id', 'id', 'integer'); |
||
| 40 | $rsm->addScalarResult('keywords', 'keywords', 'array'); |
||
| 41 | |||
| 42 | $query = $entityManager->createNativeQuery('SELECT id, keywords FROM swp_article', $rsm); |
||
| 43 | $articles = $query->getResult(); |
||
| 44 | |||
| 45 | $dbConnection = $entityManager->getConnection(); |
||
| 46 | $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('swp_keyword_id_seq'); |
||
| 47 | $newId = (int) $dbConnection->fetchColumn($nextvalQuery); |
||
| 48 | |||
| 49 | $keywords = []; |
||
| 50 | foreach ($articles as $article) { |
||
| 51 | foreach ($article['keywords'] as $articleKeyword) { |
||
| 52 | if (!\array_key_exists($articleKeyword, $keywords)) { |
||
| 53 | $keywords[$articleKeyword] = [ |
||
| 54 | 'name' => $articleKeyword, |
||
| 55 | 'id' => $newId, |
||
| 56 | ]; |
||
| 57 | |||
| 58 | $this->addSql('INSERT INTO swp_keyword (id, slug, name) VALUES (:id, :slug, :name)', [ |
||
| 59 | 'id' => $newId, |
||
| 60 | 'slug' => Transliterator::urlize($articleKeyword), |
||
| 61 | 'name' => $articleKeyword, |
||
| 62 | ]); |
||
| 63 | ++$newId; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($articles as $article) { |
||
| 69 | foreach ($article['keywords'] as $articleKeyword) { |
||
| 70 | if (array_key_exists($articleKeyword, $keywords)) { |
||
| 71 | $this->addSql( |
||
| 72 | 'INSERT INTO swp_article_keyword (article_id, keyword_id) VALUES (:article_id, :keyword_id)', |
||
| 73 | [ |
||
| 74 | 'article_id' => $article['id'], |
||
| 75 | 'keyword_id' => $keywords[$articleKeyword]['id'], |
||
| 76 | ] |
||
| 77 | ); |
||
| 78 | ++$newId; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->addSql('ALTER TABLE swp_article DROP keywords'); |
||
| 84 | } |
||
| 85 | |||
| 93 |