Completed
Push — master ( 9fe4d8...85fc2a )
by Tim
10s
created

SimpleDeploymentService::loadContainerInstances()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
/**
4
 * AppserverIo\SingleApp\Core\Api\SimpleDeploymentService
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\Api;
22
23
use AppserverIo\Properties\PropertiesInterface;
24
use AppserverIo\Appserver\Core\Api\DeploymentService;
25
use AppserverIo\Appserver\Core\Utilities\SystemPropertyKeys;
26
use AppserverIo\Appserver\Core\Interfaces\ContainerInterface;
27
28
/**
29
 * A service providing functionality for a single app deployment.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2015 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/single-app
35
 * @link      http://www.appserver.io
36
 */
37
class SimpleDeploymentService extends DeploymentService
38
{
39
40
    /**
41
     * Prepare's the system properties for the actual mode, which is the runner mode in our case.
42
     *
43
     * @param \AppserverIo\Properties\PropertiesInterface $properties The properties to prepare
44
     * @param string                                      $webappPath The path of the web application to prepare the properties with
45
     *
46
     * @return void
47
     */
48
    protected function prepareSystemProperties(PropertiesInterface $properties, $webappPath)
49
    {
50
51
        // let the parent method also prepare the properties
52
        parent::prepareSystemProperties($properties, $webappPath);
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 prepareSystemProperties() 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...
53
54
        // replace the host's application base directory with the parent directory
55
        $properties->add(SystemPropertyKeys::HOST_APP_BASE, dirname($webappPath));
56
    }
57
58
    /**
59
     * Loads the containers, defined by the applications, merges them into
60
     * the system configuration and returns the merged system configuration.
61
     *
62
     * @return \AppserverIo\Appserver\Core\Interfaces\SystemConfigurationInterface The merged system configuration
63
     */
64
    public function loadContainerInstances()
65
    {
66
67
        // load the system configuration
68
        /** @var AppserverIo\Appserver\Core\Interfaces\SystemConfigurationInterface $systemConfiguration */
69
        $systemConfiguration = $this->getSystemConfiguration();
70
71
        // if applications are NOT allowed to override the system configuration
72
        if ($systemConfiguration->getAllowApplicationConfiguration() === false) {
73
            return $systemConfiguration;
74
        }
75
76
        // load the service to validate the files
77
        /** @var AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
78
        $configurationService = $this->newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
0 ignored issues
show
Unused Code introduced by
$configurationService is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
80
        /** @var AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNodeInstance */
81
        foreach ($systemConfiguration->getContainers() as $containerNode) {
82
            $this->loadContainerInstance($containerNode, $systemConfiguration, getcwd());
0 ignored issues
show
Bug introduced by
The method loadContainerInstance() does not exist on AppserverIo\SingleApp\Co...SimpleDeploymentService. Did you maybe mean loadContainerInstances()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
83
        }
84
85
        // returns the merged system configuration
86
        return $systemConfiguration;
87
    }
88
89
    /**
90
     * Initializes the available application contexts and returns them.
91
     *
92
     * @param \AppserverIo\Appserver\Core\Interfaces\ContainerInterface $container The container we want to add the applications to
93
     *
94
     * @return array The array with the application contexts
95
     */
96
    public function loadContextInstancesByWorkingDir(ContainerInterface $container)
97
    {
98
99
        // initialize the array for the context instances
100
        $contextInstances = array();
101
102
        // attach the context to the context instances
103
        $context = $this->loadContextInstance($container, getcwd());
0 ignored issues
show
Bug introduced by
The method loadContextInstance() does not exist on AppserverIo\SingleApp\Co...SimpleDeploymentService. Did you maybe mean loadContextInstancesByWorkingDir()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
104
        $contextInstances[$context->getName()] = $context;
105
106
        // return the array with the context instances
107
        return $contextInstances;
108
    }
109
}
110