Conditions | 9 |
Paths | 11 |
Total Lines | 82 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 3 | Features | 1 |
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 |
||
31 | public function render(ElementInterface $element) |
||
32 | { |
||
33 | if (!$element instanceof Captcha) { |
||
34 | throw new Exception\InvalidArgumentException(sprintf( |
||
35 | '%s expects a valid implementation of Zend\Form\Element\Captcha; received "%s"', |
||
36 | __METHOD__, |
||
37 | (is_object($element) ? get_class($element) : gettype($element)) |
||
38 | )); |
||
39 | } |
||
40 | $captcha = $element->getCaptcha(); |
||
41 | |||
42 | if ($captcha === null || !$captcha instanceof AdapterInterface) { |
||
43 | throw new Exception\DomainException(sprintf( |
||
44 | '%s requires that the element has a "captcha" attribute implementing %s; none found', |
||
45 | __METHOD__, |
||
46 | AdapterInterface::class |
||
47 | )); |
||
48 | } |
||
49 | |||
50 | /* @var $service \ReCaptcha2\Captcha\ServiceInterface */ |
||
51 | $service = $captcha->getService(); |
||
52 | |||
53 | $siteKey = $service->getSiteKey(); |
||
54 | if ($siteKey === null) { |
||
55 | throw new Exception\DomainException('Missing site key'); |
||
56 | } |
||
57 | |||
58 | $params = array_filter($service->getParams(), function ($param) { |
||
59 | return $param !== null; |
||
60 | }); |
||
61 | |||
62 | $apiPath = $service->getServiceUri(); |
||
63 | $scriptPath = sprintf('%s.js', $apiPath); |
||
64 | $queryString = ''; |
||
65 | if (!empty($params)) { |
||
66 | $queryString = http_build_query($params, '', '&'); |
||
67 | $scriptPath .= '?' . $queryString; |
||
68 | } |
||
69 | |||
70 | $attributes = $element->getAttributes(); |
||
71 | if ($attributes instanceof Traversable) { |
||
72 | $attributes = iterator_to_array($attributes); |
||
73 | } |
||
74 | |||
75 | $attributes['class'] = 'g-recaptcha'; |
||
76 | $attributes['data-sitekey'] = $siteKey; |
||
77 | $attributeString = $this->createAttributesString($attributes); |
||
78 | |||
79 | $template = <<<HTML |
||
80 | <input type="hidden" name="%s" value="1"> |
||
81 | <div %s></div> |
||
82 | <script src="%s" async defer></script> |
||
83 | HTML; |
||
84 | $markup = sprintf($template, $element->getName(), $attributeString, $scriptPath); |
||
85 | |||
86 | $queryString .= ($queryString ? '&' : ''); |
||
87 | $markup .= <<<HTML |
||
88 | <noscript> |
||
89 | <div style="width: 302px; height: 422px;"> |
||
90 | <div style="width: 302px; height: 422px; position: relative;"> |
||
91 | <div style="width: 302px; height: 422px; position: absolute;"> |
||
92 | <iframe src="{$apiPath}/fallback?{$queryString}k={$siteKey}" |
||
93 | frameborder="0" scrolling="no" |
||
94 | style="width: 302px; height:422px; border-style: none;"> |
||
95 | </iframe> |
||
96 | </div> |
||
97 | <div style="width: 300px; height: 60px; border-style: none; |
||
98 | bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; |
||
99 | background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;"> |
||
100 | <textarea id="g-recaptcha-response" name="g-recaptcha-response" |
||
101 | class="g-recaptcha-response" |
||
102 | style="width: 250px; height: 40px; border: 1px solid #c1c1c1; |
||
103 | margin: 10px 25px; padding: 0px; resize: none;" > |
||
104 | </textarea> |
||
105 | </div> |
||
106 | </div> |
||
107 | </div> |
||
108 | </noscript> |
||
109 | HTML; |
||
110 | |||
111 | return $markup; |
||
112 | } |
||
113 | } |
||
114 |