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