TemplateAssetsResolverFactory::createService()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0322

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.5226
c 0
b 0
f 0
ccs 21
cts 23
cp 0.913
cc 7
nc 64
nop 1
crap 7.0322
1
<?php
2
3
namespace Columnis\Service\Factory;
4
5
use Zend\ServiceManager\FactoryInterface;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
use Columnis\Model\TemplateAssetsResolver;
8
9
class TemplateAssetsResolverFactory implements FactoryInterface
10
{
11
    /**
12
     * {@inheritDoc}
13
     *
14
     * @return TemplateAssetsResolver
15
     */
16 13
    public function createService(ServiceLocatorInterface $serviceLocator)
17
    {
18 13
        $config      = $serviceLocator->get('Config');
19
        
20 13
        if (isset($config['view_manager']['template_path_stack'])) {
21 13
            $templatesPathStack = $config['view_manager']['template_path_stack'];
22 13
        }
23 13
        if (isset($config['asset_manager']['resolver_configs']['paths'])) {
24 13
            $assetsPaths = $config['asset_manager']['resolver_configs']['paths'];
25 13
        }
26 13
        $templateAssetsResolver = new TemplateAssetsResolver($assetsPaths, $templatesPathStack);
0 ignored issues
show
Bug introduced by
The variable $assetsPaths does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $templatesPathStack does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
27
28 13
        $resolverCfg = $config['template_assets_resolver'];
29 13
        if (isset($resolverCfg['match_patterns']['template_name'])) {
30 13
            $templateAssetsResolver->setPatternTemplateName($resolverCfg['match_patterns']['template_name']);
31 13
        }
32 13
        if (isset($resolverCfg['match_patterns']['global_asset'])) {
33 13
            $templateAssetsResolver->setPatternGlobalAssets($resolverCfg['match_patterns']['global_asset']);
34 13
        }
35 13
        if (isset($resolverCfg['global_folder_name'])) {
36 13
            $templateAssetsResolver->setGlobalFolderName($resolverCfg['global_folder_name']);
37 13
        }
38 13
        if (isset($resolverCfg['public_path'])) {
39
            $templateAssetsResolver->setPublicPath($resolverCfg['public_path']);
40
        }
41
        
42
        
43 13
        return $templateAssetsResolver;
44
    }
45
}
46