| Conditions | 12 |
| Paths | 20 |
| Total 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 |
||
| 61 | public function reverseTransform($media) |
||
| 62 | { |
||
| 63 | if (!$media instanceof MediaInterface) { |
||
| 64 | return $media; |
||
| 65 | } |
||
| 66 | |||
| 67 | $binaryContent = $media->getBinaryContent(); |
||
| 68 | |||
| 69 | // no binary |
||
| 70 | if (empty($binaryContent)) { |
||
| 71 | // and no media id |
||
| 72 | if (null === $media->getId() && $this->options['empty_on_new']) { |
||
| 73 | return; |
||
| 74 | } elseif ($media->getId()) { |
||
| 75 | return $media; |
||
| 76 | } |
||
| 77 | |||
| 78 | $media->setProviderStatus(MediaInterface::STATUS_PENDING); |
||
| 79 | $media->setProviderReference(MediaInterface::MISSING_BINARY_REFERENCE); |
||
| 80 | |||
| 81 | return $media; |
||
| 82 | } |
||
| 83 | |||
| 84 | // create a new media to avoid erasing other media or not ... |
||
| 85 | $newMedia = $this->options['new_on_update'] ? new $this->class() : $media; |
||
| 86 | |||
| 87 | $newMedia->setProviderName($media->getProviderName()); |
||
| 88 | $newMedia->setContext($media->getContext()); |
||
| 89 | $newMedia->setBinaryContent($binaryContent); |
||
| 90 | |||
| 91 | if (!$newMedia->getProviderName() && $this->options['provider']) { |
||
| 92 | $newMedia->setProviderName($this->options['provider']); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (!$newMedia->getContext() && $this->options['context']) { |
||
| 96 | $newMedia->setContext($this->options['context']); |
||
| 97 | } |
||
| 98 | |||
| 99 | $provider = $this->pool->getProvider($newMedia->getProviderName()); |
||
| 100 | |||
| 101 | try { |
||
| 102 | $provider->transform($newMedia); |
||
| 103 | } catch (\Throwable $e) { |
||
| 104 | // #1107 We must never throw an exception here. |
||
| 105 | // An exception here would prevent us to provide meaningful errors through the Form |
||
| 106 | // Error message inspired from Monolog\ErrorHandler |
||
| 107 | $this->logger->error( |
||
| 108 | sprintf('Caught Exception %s: "%s" at %s line %s', \get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), |
||
| 109 | ['exception' => $e] |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $newMedia; |
||
| 114 | } |
||
| 115 | |||
| 131 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: