| Conditions | 7 |
| Paths | 13 |
| Total Lines | 88 |
| Code Lines | 54 |
| 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 |
||
| 50 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 51 | { |
||
| 52 | $isoCodePath = __DIR__.'/../../../vendor/ronanguilloux/isocodes'; |
||
| 53 | if (!file_exists($isoCodePath.'/.git')) { |
||
| 54 | $output->writeln( |
||
| 55 | '<error>No .git directory found on ronanguilloux/isocodes package. Please run rm -r vendor/ronanguilloux/isocodes && composer update ronanguilloux/isocodes --prefer-source command.</error>' |
||
|
|
|||
| 56 | ); |
||
| 57 | |||
| 58 | return 1; |
||
| 59 | } |
||
| 60 | |||
| 61 | $fqcnRepository = new FqcnRepository(new FileRepository(), new ParserFactory()); |
||
| 62 | |||
| 63 | $isoCodesClasses = array_diff( |
||
| 64 | array_map(function ($fqcn) { |
||
| 65 | return str_replace('IsoCodes\\', '', $fqcn); |
||
| 66 | }, $fqcnRepository->findIn(__DIR__.'/../../../vendor/ronanguilloux/isocodes/src/IsoCodes')), |
||
| 67 | $this->excludedClasses |
||
| 68 | ); |
||
| 69 | |||
| 70 | $constraintClasses = array_filter(array_map(function ($fqcn) { |
||
| 71 | return str_replace('SLLH\\IsoCodesValidator\\Constraints\\', '', $fqcn); |
||
| 72 | }, $fqcnRepository->findIn(__DIR__.'/../../../src/Constraints')), function ($className) { |
||
| 73 | return !empty(trim($className)); |
||
| 74 | }); |
||
| 75 | |||
| 76 | $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/../..')); |
||
| 77 | |||
| 78 | $xliffLoader = new XliffFileLoader(); |
||
| 79 | $xliffDumper = new XliffFileDumper(); |
||
| 80 | |||
| 81 | /** @var MessageCatalogue[] $catalogues */ |
||
| 82 | $catalogues = []; |
||
| 83 | foreach (Finder::create()->in(__DIR__.'/../../../src/translations') as $fileInfo) { |
||
| 84 | $locale = explode('.', $fileInfo->getBasename())[1]; |
||
| 85 | $catalogues[$locale] = $xliffLoader->load($fileInfo->getRealPath(), $locale, 'validators'); |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach (array_udiff($isoCodesClasses, $constraintClasses, 'strcasecmp') as $className) { |
||
| 89 | $classVersion = str_replace( |
||
| 90 | 'v', |
||
| 91 | '', |
||
| 92 | exec("git -C ${isoCodePath} tag --list --contains $(git -C ${isoCodePath} log --pretty=format:\"%h\" --diff-filter=A src/IsoCodes/${className}.php) | head -n 1") |
||
| 93 | ); |
||
| 94 | $classMessage = 'This value is not a valid '.strtoupper($className).'.'; |
||
| 95 | |||
| 96 | $output->writeln("Generate <comment>${className}</comment> constraint (<info>v${classVersion}</info>)."); |
||
| 97 | |||
| 98 | file_put_contents( |
||
| 99 | __DIR__."/../../../src/Constraints/${className}.php", |
||
| 100 | $twig->render('templates/Constraint.php.twig', [ |
||
| 101 | 'class_name' => $className, |
||
| 102 | 'class_message' => $classMessage, |
||
| 103 | 'class_version' => $classVersion, |
||
| 104 | ]) |
||
| 105 | ); |
||
| 106 | |||
| 107 | file_put_contents( |
||
| 108 | __DIR__."/../../../tests/Constraints/${className}ValidatorTest.php", |
||
| 109 | $twig->render('templates/ConstraintValidatorTest.php.twig', [ |
||
| 110 | 'class_name' => $className, |
||
| 111 | ]) |
||
| 112 | ); |
||
| 113 | |||
| 114 | foreach ($catalogues as $locale => $catalogue) { |
||
| 115 | $catalogue->set( |
||
| 116 | $classMessage, |
||
| 117 | 'en' === $locale |
||
| 118 | ? $classMessage |
||
| 119 | : Inflector::tableize($className) |
||
| 120 | , |
||
| 121 | 'validators' |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | foreach ($catalogues as $locale => $catalogue) { |
||
| 127 | $fileContents = $xliffDumper->formatCatalogue($catalogue, 'validators', [ |
||
| 128 | 'default_locale' => 'en', |
||
| 129 | ]); |
||
| 130 | file_put_contents( |
||
| 131 | __DIR__."/../../../src/translations/validators.${locale}.xlf", |
||
| 132 | str_replace(' ', ' ', $fileContents) |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | return 0; |
||
| 137 | } |
||
| 138 | } |
||
| 139 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.