Conditions | 13 |
Paths | 144 |
Total Lines | 120 |
Code Lines | 60 |
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 |
||
93 | public function startup() |
||
94 | { |
||
95 | parent::startup(); |
||
96 | |||
97 | // user-supplied "wsdl" and "options" parameters |
||
98 | $wsdl = $this->getParameter('wsdl'); |
||
99 | if (!$wsdl) { |
||
100 | // no wsdl was specified, that means we generate one from the annotations in routing.xml |
||
101 | $wsdl = $this->context->getRouting()->getWsdlPath(); |
||
|
|||
102 | } |
||
103 | $this->setParameter('wsdl', $wsdl); |
||
104 | |||
105 | // get the name of the class to use for the client, defaults to PHP's own "SoapClient" |
||
106 | $soapClientClass = $this->getParameter('soap_client_class', 'SoapClient'); |
||
107 | $soapClientOptions = $this->getParameter('soap_client_options', array()); |
||
108 | // get the name of the class to use for the server, defaults to PHP's own "SoapServer" |
||
109 | $soapServerClass = $this->getParameter('soap_server_class', 'SoapServer'); |
||
110 | $soapServerOptions = $this->getParameter('soap_server_options', array()); |
||
111 | // get the name of the class to use for handling soap calls, defaults to Agavi's "AgaviSoapDispatcherCallHandler" |
||
112 | $soapHandlerClass = $this->getParameter('soap_handler_class', 'AgaviSoapDispatcherCallHandler'); |
||
113 | |||
114 | // force client's soap version to be the same as the server's |
||
115 | if (isset($soapServerOptions['soap_version'])) { |
||
116 | $soapClientOptions['soap_version'] = $soapServerOptions['soap_version']; |
||
117 | } |
||
118 | |||
119 | // force client's cache_wsdl setting to be the same as the server's |
||
120 | if (isset($soapServerOptions['cache_wsdl'])) { |
||
121 | // and cast it to an int |
||
122 | $soapServerOptions['cache_wsdl'] = (int)$soapServerOptions['cache_wsdl']; |
||
123 | $soapClientOptions['cache_wsdl'] = $soapServerOptions['cache_wsdl']; |
||
124 | } |
||
125 | |||
126 | if (isset($soapServerOptions['features'])) { |
||
127 | // cast this to an int |
||
128 | $soapServerOptions['features'] = (int)$soapServerOptions['features']; |
||
129 | } |
||
130 | |||
131 | // create a client, so we can grab the functions and types defined in the wsdl (not possible from the server, duh) |
||
132 | $this->soapClient = new $soapClientClass($wsdl, $soapClientOptions); |
||
133 | |||
134 | if ($this->getParameter('auto_classmap')) { |
||
135 | // we have to create a classmap automatically. |
||
136 | // to do that, we read the defined types, and set identical values for type and class name. |
||
137 | $classmap = array(); |
||
138 | |||
139 | // with an optional prefix, of course. |
||
140 | $prefix = $this->getParameter('auto_classmap_prefix', ''); |
||
141 | |||
142 | foreach ($this->soapClient->__getTypes() as $definition) { |
||
143 | if (preg_match('/^struct (\S+) \{$/m', $definition, $matches)) { |
||
144 | $classmap[$matches[1]] = $prefix . $matches[1]; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | if (isset($soapServerOptions['classmap'])) { |
||
149 | $classmap = array_merge((array) $classmap, $soapServerOptions['classmap']); |
||
150 | } |
||
151 | |||
152 | $soapServerOptions['classmap'] = $classmap; |
||
153 | } |
||
154 | |||
155 | // create a server |
||
156 | $this->soapServer = new $soapServerClass($wsdl, $soapServerOptions); |
||
157 | |||
158 | $newSoapHandlerClass = $soapHandlerClass . 'WithAutoHeaders'; |
||
159 | |||
160 | // build the special extension class to the handler that contains methods for each of the headers |
||
161 | if ($this->getParameter('auto_headers', true)) { |
||
162 | // the cache filename we'll be using |
||
163 | $cache = ConfigCache::getCacheName($soapHandlerClass, $this->context->getName()); |
||
164 | |||
165 | if (ConfigCache::isModified($wsdl, $cache)) { |
||
166 | $doc = new \DOMDocument(); |
||
167 | $doc->load($wsdl); |
||
168 | $xpath = new \DOMXPath($doc); |
||
169 | $xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/wsdl/soap/'); |
||
170 | |||
171 | $code = array(); |
||
172 | |||
173 | $code[] = '<?php'; |
||
174 | $code[] = sprintf('class %s extends %s {', $newSoapHandlerClass, $soapHandlerClass); |
||
175 | $code[] = ' protected $rd;'; |
||
176 | $code[] = ' public function __construct(Context $context) {'; |
||
177 | $code[] = ' parent::__construct($context);'; |
||
178 | $code[] = ' $this->rd = $this->context->getRequest()->getRequestData();'; |
||
179 | $code[] = ' }'; |
||
180 | |||
181 | $headers = array(); |
||
182 | |||
183 | /** @var \DOMElement $header */ |
||
184 | foreach ($xpath->query('//soap:header') as $header) { |
||
185 | $name = $header->getAttribute('part'); |
||
186 | |||
187 | if (in_array($name, $headers)) { |
||
188 | continue; |
||
189 | } |
||
190 | $headers[] = $name; |
||
191 | |||
192 | $code[] = sprintf(' public function %s($value) {', $name); |
||
193 | $code[] = sprintf(' $this->rd->setHeader(%s, $value);', var_export($name, true)); |
||
194 | $code[] = ' }'; |
||
195 | } |
||
196 | |||
197 | $code[] = '}'; |
||
198 | $code[] = '?>'; |
||
199 | |||
200 | $code = implode("\n", $code); |
||
201 | |||
202 | ConfigCache::writeCacheFile($soapHandlerClass, $cache, $code); |
||
203 | } |
||
204 | |||
205 | include($cache); |
||
206 | } |
||
207 | |||
208 | // give it a class that handles method calls |
||
209 | // that class uses __call |
||
210 | // the class ctor gets the context as the first argument |
||
211 | $this->soapServer->setClass($newSoapHandlerClass, $this->context); |
||
212 | } |
||
213 | /** |
||
262 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: