Completed
Push — master ( 4c21a7...9fe4d8 )
by Tim
7s
created

SimpleDeployment::getDeploymentService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * AppserverIo\SingleApp\Core\SimpleDeployment
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/single-app
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\SingleApp\Core;
22
23
use AppserverIo\Appserver\Core\GenericDeployment;
24
25
/**
26
 * Deployment implementation for container's with a single app configuration.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/single-app
32
 * @link      http://www.appserver.io
33
 */
34
class SimpleDeployment extends GenericDeployment
35
{
36
37
    /**
38
     * Returns the deployment service instance.
39
     *
40
     * @return \AppserverIo\Appserver\Core\Api\DeploymentService The deployment service instance
41
     */
42
    public function getDeploymentService()
43
    {
44
        if ($this->deploymentService == null) {
45
            $this->deploymentService = $this->newService('AppserverIo\SingleApp\Core\Api\SimpleDeploymentService');
46
        }
47
        return $this->deploymentService;
48
    }
49
50
    /**
51
     * Return's the container's directory with applications to be deployed.
52
     *
53
     * @return string The container's application base directory
54
     */
55
    protected function getAppBase()
56
    {
57
        return getcwd();
58
    }
59
60
    /**
61
     * Load's and return's the context instances for the container.
62
     *
63
     * @return array The array with the container's context instances
64
     */
65
    protected function loadContextInstances()
66
    {
67
        return $this->getDeploymentService()->loadContextInstancesByWorkingDir($this->getContainer());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class AppserverIo\Appserver\Core\Api\DeploymentService as the method loadContextInstancesByWorkingDir() does only exist in the following sub-classes of AppserverIo\Appserver\Core\Api\DeploymentService: AppserverIo\SingleApp\Co...SimpleDeploymentService. 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...
68
    }
69
}
70