Conditions | 4 |
Paths | 4 |
Total Lines | 65 |
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 |
||
49 | public function handle(ImportXmlMandateFile $command): void |
||
50 | { |
||
51 | $this->dispatcher->dispatch( |
||
52 | new Event\InfoEvent( |
||
53 | "<info>Importing mandates from {$command->getFile()->getFilename()}</info>", |
||
54 | ['filename' => $command->getFile()->getFilename()] |
||
55 | ) |
||
56 | ); |
||
57 | |||
58 | foreach ($this->xmlMandateCompiler->compileFile($command->getFile()) as $xmlMandate) { |
||
59 | try { |
||
60 | $this->commandBus->handle( |
||
61 | new CommandBus\AddDonor( |
||
62 | new NewDonor( |
||
63 | MandateSources::MANDATE_SOURCE_ONLINE_FORM, |
||
64 | $xmlMandate->payerNumber, |
||
65 | $xmlMandate->account, |
||
66 | $xmlMandate->donorId, |
||
67 | $xmlMandate->donationAmount |
||
68 | ) |
||
69 | ) |
||
70 | ); |
||
71 | } catch (DonorAlreadyExistsException $exception) { |
||
72 | // Dispatching error means that failure can be picked up in an outer layer |
||
73 | $this->dispatcher->dispatch( |
||
74 | new Event\ErrorEvent( |
||
75 | $exception->getMessage(), |
||
76 | [ |
||
77 | 'payer_number' => $xmlMandate->payerNumber, |
||
78 | 'donor_id' => $xmlMandate->donorId->format('CS-sk') |
||
79 | ] |
||
80 | ) |
||
81 | ); |
||
82 | |||
83 | continue; |
||
84 | } |
||
85 | |||
86 | $donor = $this->donorRepository->requireByPayerNumber($xmlMandate->payerNumber); |
||
87 | |||
88 | $this->commandBus->handle( |
||
89 | new CommandBus\UpdateState($donor, NewMandate::getStateId(), 'Mandate added from xml') |
||
90 | ); |
||
91 | |||
92 | $this->commandBus->handle(new CommandBus\UpdateName($donor, $xmlMandate->name)); |
||
93 | |||
94 | $this->commandBus->handle(new CommandBus\UpdatePostalAddress($donor, new PostalAddress( |
||
95 | $xmlMandate->address['line1'], |
||
96 | $xmlMandate->address['line2'], |
||
97 | $xmlMandate->address['line3'], |
||
98 | $xmlMandate->address['postalCode'], |
||
99 | $xmlMandate->address['postalCity'] |
||
100 | ))); |
||
101 | |||
102 | $this->commandBus->handle(new CommandBus\UpdateEmail($donor, $xmlMandate->email)); |
||
103 | |||
104 | $this->commandBus->handle(new CommandBus\UpdatePhone($donor, $xmlMandate->phone)); |
||
105 | |||
106 | $this->commandBus->handle(new CommandBus\UpdateComment($donor, $xmlMandate->comment)); |
||
107 | |||
108 | foreach ($xmlMandate->attributes as $attrKey => $attrValue) { |
||
109 | $this->commandBus->handle(new CommandBus\UpdateAttribute($donor, $attrKey, $attrValue)); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $this->dispatcher->dispatch(new Event\XmlMandateFileImported($command->getFile())); |
||
114 | } |
||
116 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: