Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 44 | class InitialContext |
||
| 45 | { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Lexer constants. |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | const T_CLASS = 0; |
||
| 53 | const T_COLON = 1; |
||
| 54 | const T_SCHEME = 2; |
||
| 55 | const T_APPLICATION_SCOPE = 3; |
||
| 56 | const T_GLOBAL_SCOPE = 4; |
||
| 57 | const T_SEPARATOR = 5; |
||
| 58 | const T_INTERFACE = 6; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The configuration properties for this context. |
||
| 62 | * |
||
| 63 | * @var \AppserverIo\Properties\PropertiesInterface |
||
| 64 | */ |
||
| 65 | protected $properties; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The lexer used to parse the JNDI style bean names. |
||
| 69 | * |
||
| 70 | * @param \Phlexy\Lexer $lexer the lexer instance |
||
| 71 | */ |
||
| 72 | protected $lexer; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The application instance the context is bound to. |
||
| 76 | * |
||
| 77 | * @var \AppserverIo\Psr\Application\ApplicationInterface |
||
| 78 | */ |
||
| 79 | protected $application; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The servlet request instance the context is bound to. |
||
| 83 | * |
||
| 84 | * @var \AppserverIo\Psr\Servlet\ServletRequestInterface |
||
| 85 | */ |
||
| 86 | protected $servletRequest; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The default properties for the context configuration. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $defaultProperties = array( |
||
| 94 | 'transport' => 'http', |
||
| 95 | 'scheme' => 'php', |
||
| 96 | 'user' => 'appserver', |
||
| 97 | 'pass' => 'appserver.i0', |
||
| 98 | 'host' => '127.0.0.1', |
||
| 99 | 'port' => '8585', |
||
| 100 | 'scope' => 'app', |
||
| 101 | 'indexFile' => 'index.pc', |
||
| 102 | 'interface' => EnterpriseBeanResourceIdentifier::LOCAL_INTERFACE |
||
| 103 | ); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Initialize the initial context with the values of the passed properties. |
||
| 107 | * |
||
| 108 | * @param \AppserverIo\Properties\PropertiesInterface $properties The configuration properties |
||
| 109 | */ |
||
| 110 | 11 | public function __construct(PropertiesInterface $properties = null) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * The configuration properties for this context. |
||
| 146 | * |
||
| 147 | * @param \AppserverIo\Properties\PropertiesInterface $properties The configuration properties |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | 11 | public function injectProperties(PropertiesInterface $properties) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * The lexer used to parse the JNDI style bean names. |
||
| 158 | * |
||
| 159 | * @param \Phlexy\Lexer $lexer the lexer instance |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | 11 | public function injectLexer(Lexer $lexer) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * The application instance this context is bound to. |
||
| 170 | * |
||
| 171 | * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | public function injectApplication(ApplicationInterface $application) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * The servlet request instance for binding stateful session beans to. |
||
| 182 | * |
||
| 183 | * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The servlet request instance |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | public function injectServletRequest(ServletRequestInterface $servletRequest) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the initial context configuration properties. |
||
| 194 | * |
||
| 195 | * @return \AppserverIo\Properties\PropertiesInterface The configuration properties |
||
| 196 | */ |
||
| 197 | 11 | public function getProperties() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Returns the lexer used to parse the JNDI style bean names. |
||
| 204 | * |
||
| 205 | * @return \Phlexy\Lexer The lexer instance |
||
| 206 | */ |
||
| 207 | 11 | public function getLexer() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Returns the application instance this context is bound to. |
||
| 214 | * |
||
| 215 | * @return \AppserverIo\Psr\Application\ApplicationInterface The application instance |
||
| 216 | */ |
||
| 217 | public function getApplication() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the servlet request instance for binding stateful session beans to. |
||
| 224 | * |
||
| 225 | * @return \AppserverIo\Psr\Servlet\ServletRequestInterface The servlet request instance |
||
| 226 | */ |
||
| 227 | public function getServletRequest() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the requested enterprise bean instance remote/local. |
||
| 234 | * |
||
| 235 | * Example URL for loading a local instance of the UserProcessor session bean: |
||
| 236 | * |
||
| 237 | * php:app/UserProcessor/local |
||
| 238 | * |
||
| 239 | * @param string $name The name of the requested enterprise bean |
||
| 240 | * @param string $sessionId The session-ID, necessary for lookup stateful session beans |
||
| 241 | * |
||
| 242 | * @return object The requested enterprise bean instance |
||
| 243 | * |
||
| 244 | * @throws \AppserverIo\Psr\Naming\NamingException Is thrown if we can't lookup the enterprise bean with the passed identifier |
||
| 245 | */ |
||
| 246 | public function lookup($name, $sessionId = null) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Prepares a new resource identifier instance from the passed resource name, that has to be |
||
| 279 | * a valid URL. |
||
| 280 | * |
||
| 281 | * @param string $resourceName The URL with the resource information |
||
| 282 | * |
||
| 283 | * @return \AppserverIo\Psr\Naming\EnterpriseBeanResourceIdentifier The initialized resource identifier |
||
| 284 | * |
||
| 285 | * @throws \AppserverIo\Psr\Naming\NamingException Is thrown if we can't find the necessary application context |
||
| 286 | */ |
||
| 287 | 11 | public function prepareResourceIdentifier($resourceName) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Makes a remote lookup for the URL containing the information of the requested bean. |
||
| 324 | * |
||
| 325 | * @param \AppserverIo\Psr\Naming\ResourceIdentifier $resourceIdentifier The resource identifier with the requested bean information |
||
| 326 | * @param string $sessionId The session-ID, necessary for lookup stateful session beans |
||
| 327 | * |
||
| 328 | * @return object The been proxy instance |
||
| 329 | */ |
||
| 330 | protected function doRemoteLookup(ResourceIdentifier $resourceIdentifier, $sessionId = null) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Makes a local lookup for the bean with the passed class name. |
||
| 361 | * |
||
| 362 | * @param \AppserverIo\Psr\Naming\ResourceIdentifier $resourceIdentifier The resource identifier with the requested bean information |
||
| 363 | * @param string $sessionId The session-ID, necessary for lookup stateful session beans |
||
| 364 | * |
||
| 365 | * @return object The bean proxy instance |
||
| 366 | */ |
||
| 367 | protected function doLocalLookup(ResourceIdentifier $resourceIdentifier, $sessionId = null) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Finally this method does the lookup for the passed resource identifier |
||
| 387 | * using the also passed connection. |
||
| 388 | * |
||
| 389 | * @param \AppserverIo\Psr\Naming\ResourceIdentifier $resourceIdentifier The identifier for the requested bean |
||
| 390 | * @param \AppserverIo\RemoteMethodInvocation\ConnectionInterface $connection The connection we use for loading the bean |
||
| 391 | * @param string $sessionId The session-ID, necessary for lookup stateful session beans |
||
| 392 | * |
||
| 393 | * @return object The been proxy instance |
||
| 394 | */ |
||
| 395 | protected function doLookup(ResourceIdentifier $resourceIdentifier, ConnectionInterface $connection, $sessionId = null) |
||
| 432 | } |
||
| 433 |
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: