| Conditions | 7 |
| Paths | 18 |
| Total Lines | 65 |
| Code Lines | 38 |
| 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 |
||
| 107 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 108 | { |
||
| 109 | $io = new SymfonyStyle($input, $output); |
||
| 110 | $targets = $this->getTargetsLists(); |
||
| 111 | |||
| 112 | //Convert for every class target |
||
| 113 | foreach ($targets as $class => $properties) { |
||
| 114 | $io->section(sprintf('Convert entities of class %s', $class)); |
||
| 115 | $io->note(sprintf( |
||
| 116 | 'Search for entities of type %s that need conversion', |
||
| 117 | $class |
||
| 118 | )); |
||
| 119 | //Determine which entities of this type we need to modify |
||
| 120 | /** @var EntityRepository $repo */ |
||
| 121 | $repo = $this->em->getRepository($class); |
||
| 122 | $qb = $repo->createQueryBuilder('e') |
||
| 123 | ->select('e'); |
||
| 124 | //Add fields criteria |
||
| 125 | foreach ($properties as $key => $property) { |
||
| 126 | $qb->orWhere('e.'.$property.' LIKE ?'.$key); |
||
| 127 | $qb->setParameter($key, static::BBCODE_CRITERIA); |
||
| 128 | } |
||
| 129 | |||
| 130 | //Fetch resulting classes |
||
| 131 | $results = $qb->getQuery()->getResult(); |
||
| 132 | $io->note(sprintf('Found %d entities, that need to be converted!', count($results))); |
||
|
|
|||
| 133 | |||
| 134 | //In verbose mode print the names of the entities |
||
| 135 | foreach ($results as $result) { |
||
| 136 | /** @var AbstractNamedDBElement $result */ |
||
| 137 | $io->writeln( |
||
| 138 | 'Convert entity: '.$result->getName().' ('.get_class($result).': '.$result->getID().')', |
||
| 139 | OutputInterface::VERBOSITY_VERBOSE |
||
| 140 | ); |
||
| 141 | foreach ($properties as $property) { |
||
| 142 | //Retrieve bbcode from entity |
||
| 143 | $bbcode = $this->propertyAccessor->getValue($result, $property); |
||
| 144 | //Check if the current property really contains BBCode |
||
| 145 | if (!preg_match(static::BBCODE_REGEX, $bbcode)) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | $io->writeln( |
||
| 149 | 'BBCode (old): ' |
||
| 150 | .str_replace('\n', ' ', substr($bbcode, 0, 255)), |
||
| 151 | OutputInterface::VERBOSITY_VERY_VERBOSE |
||
| 152 | ); |
||
| 153 | $markdown = $this->converter->convert($bbcode); |
||
| 154 | $io->writeln( |
||
| 155 | 'Markdown (new): ' |
||
| 156 | .str_replace('\n', ' ', substr($markdown, 0, 255)), |
||
| 157 | OutputInterface::VERBOSITY_VERY_VERBOSE |
||
| 158 | ); |
||
| 159 | $io->writeln('', OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 160 | $this->propertyAccessor->setValue($result, $property, $markdown); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | //If we are not in dry run, save changes to DB |
||
| 166 | if (!$input->getOption('dry-run')) { |
||
| 167 | $this->em->flush(); |
||
| 168 | $io->success('Changes saved to DB successfully!'); |
||
| 169 | } |
||
| 170 | |||
| 171 | return 0; |
||
| 172 | } |
||
| 174 |