Conditions | 8 |
Paths | 8 |
Total Lines | 74 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Tests | 31 |
CRAP Score | 8 |
Changes | 11 | ||
Bugs | 2 | Features | 3 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
71 | 4 | protected function import(OutputInterface $output, $importerId, $sourceProviderId, $sourceId, $context=null, $offset=0, $limit=null, $isDryrun=false) |
|
72 | { |
||
73 | 4 | $output->writeln("Commencing ".($isDryrun?'<comment>dry-run</comment> ':'')."import using importer ".(empty($importerId)?'<comment>unknown</comment>':"<info>$importerId</info>")." with source provider <info>$sourceProviderId</info> and source id <info>$sourceId</info>"); |
|
74 | |||
75 | 4 | $sourceId = Utils::parseSourceId($sourceId); |
|
76 | 4 | $progress = new ProgressBar($output); |
|
77 | |||
78 | //set limit |
||
79 | 4 | if ($limit) { |
|
80 | 1 | $output->writeln("Limiting import to <info>$limit</info> rows."); |
|
81 | |||
82 | $this->getContainer()->get('event_dispatcher')->addListener(ImportConfigureEvent::AFTER_BUILD, function (ImportConfigureEvent $event) use ($offset, $limit) { |
||
83 | $event->getImport()->importer()->filters()->add(new OffsetFilter($offset, $limit)); |
||
84 | 1 | }); |
|
85 | } |
||
86 | |||
87 | //show discovered importer id |
||
88 | 4 | if (empty($importerId)) { |
|
89 | $this->getContainer()->get('event_dispatcher')->addListener(ImportRequestEvent::DISCOVERED, function (ImportRequestEvent $event) use ($output) { |
||
90 | $importerId = $event->getImportRequest()->getImporterId(); |
||
91 | $output->writeln("Importer discovered: <info>$importerId</info>"); |
||
92 | 2 | }); |
|
93 | } |
||
94 | |||
95 | /** @var ImportBuilder $importBuilder */ |
||
96 | 4 | $importBuilder = $this->getContainer()->get('mathielen_importengine.import.builder'); |
|
97 | |||
98 | 4 | $importRequest = new ImportRequest($sourceId, $sourceProviderId, $importerId, Utils::whoAmI().'@CLI', $context); |
|
99 | |||
100 | 4 | $import = $importBuilder->buildFromRequest($importRequest); |
|
101 | |||
102 | //apply context info from commandline |
||
103 | 4 | $importRun = $import->getRun(); |
|
104 | |||
105 | //status callback |
||
106 | 4 | $this->getContainer()->get('event_dispatcher')->addListener(ImportItemEvent::AFTER_READ, function (ImportItemEvent $event) use ($output, &$progress) { |
|
107 | /** @var ImportRun $importRun */ |
||
108 | $importRun = $event->getContext()->getRun(); |
||
109 | $stats = $importRun->getStatistics(); |
||
110 | $processed = isset($stats['processed'])?$stats['processed']:0; |
||
111 | $max = $importRun->getInfo()['count']; |
||
112 | |||
113 | if ($progress->getMaxSteps() != $max) { |
||
114 | $progress = new ProgressBar($output, $max); |
||
115 | $progress->start(); |
||
116 | } |
||
117 | |||
118 | $progress->setProgress($processed); |
||
119 | 4 | }); |
|
120 | |||
121 | /** @var ImportRunner $importRunner */ |
||
122 | 4 | $importRunner = $this->getContainer()->get('mathielen_importengine.import.runner'); |
|
123 | 4 | if ($isDryrun) { |
|
124 | 1 | $importRunner->dryRun($import); |
|
125 | } else { |
||
126 | 3 | $importRunner->run($import); |
|
127 | } |
||
128 | |||
129 | 4 | $progress->finish(); |
|
130 | 4 | $output->writeln(''); |
|
131 | 4 | $output->writeln("<info>Import done</info>"); |
|
132 | 4 | $output->writeln(''); |
|
133 | |||
134 | 4 | $this->writeStatistics($importRun->getStatistics(), new Table($output)); |
|
135 | |||
136 | 4 | $this->writeValidationViolations( |
|
137 | $import |
||
138 | 4 | ->importer() |
|
139 | 4 | ->validation() |
|
140 | 4 | ->getViolations(), |
|
141 | 4 | new Table($output)); |
|
142 | |||
143 | 4 | $output->writeln(''); |
|
144 | 4 | } |
|
145 | |||
202 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.