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