| Conditions | 10 |
| Paths | 64 |
| Total Lines | 62 |
| Code Lines | 42 |
| 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 |
||
| 19 | public function up(Schema $schema): void |
||
| 20 | { |
||
| 21 | $table = $schema->getTable('extra_field'); |
||
| 22 | if (false === $table->hasColumn('helper_text')) { |
||
| 23 | $this->addSql('ALTER TABLE extra_field ADD helper_text text DEFAULT NULL AFTER display_text'); |
||
| 24 | } |
||
| 25 | $this->addSql('ALTER TABLE extra_field_values CHANGE value value LONGTEXT DEFAULT NULL;'); |
||
| 26 | if (false === $table->hasColumn('description')) { |
||
| 27 | $this->addSql('ALTER TABLE extra_field ADD description LONGTEXT DEFAULT NULL'); |
||
| 28 | } |
||
| 29 | |||
| 30 | $table = $schema->getTable('extra_field_values'); |
||
| 31 | if (!$table->hasIndex('idx_efv_item')) { |
||
| 32 | $this->addSql('CREATE INDEX idx_efv_item ON extra_field_values (item_id)'); |
||
| 33 | } |
||
| 34 | |||
| 35 | // Migrate extra field fields |
||
| 36 | $container = $this->getContainer(); |
||
| 37 | $doctrine = $container->get('doctrine'); |
||
| 38 | $em = $doctrine->getManager(); |
||
| 39 | /** @var Connection $connection */ |
||
| 40 | $connection = $em->getConnection(); |
||
| 41 | |||
| 42 | $kernel = $container->get('kernel'); |
||
| 43 | $rootPath = $kernel->getProjectDir(); |
||
| 44 | |||
| 45 | $batchSize = self::BATCH_SIZE; |
||
| 46 | $counter = 1; |
||
| 47 | $q = $em->createQuery('SELECT v FROM Chamilo\CoreBundle\Entity\ExtraFieldValues v'); |
||
| 48 | |||
| 49 | $fieldWithFiles = [\ExtraField::FIELD_TYPE_FILE, \ExtraField::FIELD_TYPE_FILE_IMAGE]; |
||
| 50 | |||
| 51 | /** @var ExtraFieldValues $item */ |
||
| 52 | foreach ($q->toIterable() as $item) { |
||
| 53 | if (in_array($item->getField()->getFieldType(), $fieldWithFiles)) { |
||
| 54 | $path = $item->getValue(); |
||
| 55 | if (empty($path)) { |
||
| 56 | continue; |
||
| 57 | } |
||
| 58 | $filePath = $rootPath.'/app/upload/'.$path; |
||
| 59 | if (file_exists($filePath) && !is_dir($filePath)) { |
||
| 60 | $fileName = basename($path); |
||
| 61 | $mimeType = mime_content_type($filePath); |
||
| 62 | $file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
||
| 63 | $asset = new Asset(); |
||
| 64 | $asset |
||
| 65 | ->setCategory(Asset::EXTRA_FIELD) |
||
| 66 | ->setTitle($fileName) |
||
| 67 | ->setFile($file) |
||
| 68 | ; |
||
| 69 | $em->persist($asset); |
||
| 70 | $em->flush(); |
||
| 71 | $item->setValue($asset->getId()); |
||
| 72 | $em->persist($item); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | if (0 === $counter % $batchSize) { |
||
| 77 | $em->flush(); |
||
| 78 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 79 | } |
||
| 80 | $counter++; |
||
| 81 | } |
||
| 84 |