| Conditions | 8 |
| Paths | 17 |
| Total Lines | 60 |
| 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 |
||
| 60 | public function postUp(Schema $schema): void |
||
| 61 | { |
||
| 62 | $entityManager = $this->container->get('doctrine.orm.default_entity_manager'); |
||
| 63 | $entityManager->getConnection()->getConfiguration()->setSQLLogger(null); |
||
| 64 | |||
| 65 | $batchSize = 500; |
||
| 66 | $numberOfRecordsPerPage = 2000; |
||
| 67 | |||
| 68 | $totalArticles = $entityManager |
||
| 69 | ->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a') |
||
| 70 | ->getSingleScalarResult(); |
||
| 71 | |||
| 72 | $totalArticlesProcessed = 0; |
||
| 73 | $isProcessing = true; |
||
| 74 | |||
| 75 | while ($isProcessing) { |
||
| 76 | $sql = "SELECT id, extra FROM swp_article LIMIT $numberOfRecordsPerPage OFFSET $totalArticlesProcessed"; |
||
| 77 | $query = $entityManager->getConnection()->prepare($sql); |
||
| 78 | $query->execute(); |
||
| 79 | $results = $query->fetchAll(); |
||
| 80 | |||
| 81 | echo 'fetching '.$numberOfRecordsPerPage.' starting from '.$totalArticlesProcessed.PHP_EOL; |
||
| 82 | |||
| 83 | foreach ($results as $result) { |
||
| 84 | $legacyExtra = unserialize($result['extra']); |
||
| 85 | if (empty($legacyExtra)) { |
||
| 86 | ++$totalArticlesProcessed; |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $article = $entityManager->find( |
||
| 91 | Article::class, |
||
| 92 | $result['id'] |
||
| 93 | ); |
||
| 94 | |||
| 95 | foreach ($legacyExtra as $key => $extraItem) { |
||
| 96 | if (is_array($extraItem)) { |
||
| 97 | $extra = ArticleExtraEmbedField::newFromValue($key, $extraItem); |
||
| 98 | } else { |
||
| 99 | $extra = ArticleExtraTextField::newFromValue($key, $extraItem); |
||
| 100 | } |
||
| 101 | $extra->setArticle($article); |
||
| 102 | } |
||
| 103 | |||
| 104 | $entityManager->persist($extra); |
||
|
|
|||
| 105 | |||
| 106 | if (0 === ($totalArticlesProcessed % $batchSize)) { |
||
| 107 | $entityManager->flush(); |
||
| 108 | $entityManager->clear(); |
||
| 109 | } |
||
| 110 | ++$totalArticlesProcessed; |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($totalArticlesProcessed === $totalArticles) { |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | |||
| 117 | $entityManager->flush(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: