Conditions | 16 |
Paths | 34 |
Total Lines | 88 |
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 |
||
58 | public function getHelperProperties(MediaInterface $media, $format, $options = []) |
||
59 | { |
||
60 | if (isset($options['srcset'], $options['picture'])) { |
||
61 | throw new \LogicException("The 'srcset' and 'picture' options must not be used simultaneously."); |
||
62 | } |
||
63 | |||
64 | if (MediaProviderInterface::FORMAT_REFERENCE === $format) { |
||
65 | $box = $media->getBox(); |
||
66 | } else { |
||
67 | $resizerFormat = $this->getFormat($format); |
||
68 | if (false === $resizerFormat) { |
||
69 | throw new \RuntimeException(sprintf('The image format "%s" is not defined. |
||
70 | Is the format registered in your ``sonata_media`` configuration?', $format)); |
||
71 | } |
||
72 | |||
73 | $box = $this->resizer->getBox($media, $resizerFormat); |
||
74 | } |
||
75 | |||
76 | $mediaWidth = $box->getWidth(); |
||
77 | |||
78 | $params = [ |
||
79 | 'alt' => $media->getDescription() ?: $media->getName(), |
||
80 | 'title' => $media->getName(), |
||
81 | 'src' => $this->generatePublicUrl($media, $format), |
||
82 | 'width' => $mediaWidth, |
||
83 | 'height' => $box->getHeight(), |
||
84 | ]; |
||
85 | |||
86 | if (isset($options['picture'])) { |
||
87 | $pictureParams = []; |
||
88 | foreach ($options['picture'] as $key => $pictureFormat) { |
||
89 | $formatName = $this->getFormatName($media, $pictureFormat); |
||
90 | $settings = $this->getFormat($formatName); |
||
91 | $src = $this->generatePublicUrl($media, $formatName); |
||
92 | $mediaQuery = \is_string($key) |
||
93 | ? $key |
||
94 | : sprintf('(max-width: %dpx)', $this->resizer->getBox($media, $settings)->getWidth()); |
||
|
|||
95 | |||
96 | $pictureParams['source'][] = ['media' => $mediaQuery, 'srcset' => $src]; |
||
97 | } |
||
98 | |||
99 | unset($options['picture']); |
||
100 | $pictureParams['img'] = $params + $options; |
||
101 | $params = ['picture' => $pictureParams]; |
||
102 | } elseif (MediaProviderInterface::FORMAT_ADMIN !== $format) { |
||
103 | $srcSetFormats = $this->getFormats(); |
||
104 | |||
105 | if (isset($options['srcset']) && \is_array($options['srcset'])) { |
||
106 | $srcSetFormats = []; |
||
107 | foreach ($options['srcset'] as $srcSetFormat) { |
||
108 | $formatName = $this->getFormatName($media, $srcSetFormat); |
||
109 | $srcSetFormats[$formatName] = $this->getFormat($formatName); |
||
110 | } |
||
111 | unset($options['srcset']); |
||
112 | |||
113 | // Make sure the requested format is also in the srcSetFormats |
||
114 | if (!isset($srcSetFormats[$format])) { |
||
115 | $srcSetFormats[$format] = $this->getFormat($format); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | if (!isset($options['srcset'])) { |
||
120 | $srcSet = []; |
||
121 | |||
122 | foreach ($srcSetFormats as $providerFormat => $settings) { |
||
123 | // Check if format belongs to the current media's context |
||
124 | if (0 === strpos($providerFormat, $media->getContext())) { |
||
125 | $width = $this->resizer->getBox($media, $settings)->getWidth(); |
||
126 | |||
127 | $srcSet[] = sprintf('%s %dw', $this->generatePublicUrl($media, $providerFormat), $width); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // The reference format is not in the formats list |
||
132 | $srcSet[] = sprintf( |
||
133 | '%s %dw', |
||
134 | $this->generatePublicUrl($media, MediaProviderInterface::FORMAT_REFERENCE), |
||
135 | $media->getBox()->getWidth() |
||
136 | ); |
||
137 | |||
138 | $params['srcset'] = implode(', ', $srcSet); |
||
139 | } |
||
140 | |||
141 | $params['sizes'] = sprintf('(max-width: %1$dpx) 100vw, %1$dpx', $mediaWidth); |
||
142 | } |
||
143 | |||
144 | return array_merge($params, $options); |
||
145 | } |
||
146 | |||
239 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.