ConsoleRouting::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Routing;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\Core\Context;
17
18
/**
19
 * ConsoleRouting handles the routing for command line requests.
20
 *
21
 * @package    agavi
22
 * @subpackage routing
23
 *
24
 * @author     David Zülke <[email protected]>
25
 * @copyright  Authors
26
 * @copyright  The Agavi Project
27
 *
28
 * @since      1.0.0
29
 *
30
 * @version    $Id$
31
 */
32 View Code Duplication
class ConsoleRouting extends Routing
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
{
34
    /**
35
     * Initialize the routing instance.
36
     *
37
     * @param      Context $context A Context instance.
38
     * @param      array   $parameters An array of initialization parameters.
39
     *
40
     * @author     David Zülke <[email protected]>
41
     * @since      1.0.0
42
     */
43
    public function initialize(Context $context, array $parameters = array())
44
    {
45
        parent::initialize($context, $parameters);
46
        
47
        if (!$this->isEnabled()) {
48
            return;
49
        }
50
    }
51
    
52
    /**
53
     * Set the name of the called web service method as the routing input.
54
     *
55
     * @author     David Zülke <[email protected]>
56
     * @since      1.0.0
57
     */
58
    public function startup()
59
    {
60
        parent::startup();
61
        
62
        $this->input = $this->context->getRequest()->getInput();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Agavi\Request\Request as the method getInput() does only exist in the following sub-classes of Agavi\Request\Request: Agavi\Request\ConsoleRequest, Agavi\Request\SoapRequest, Agavi\Request\WebserviceRequest, Agavi\Request\XmlrpcepiphpRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
63
    }
64
}
65