GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 66c291...444f0e )
by Bruno
03:49
created

PlaceholdersRepository::getFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Ciandt\Behat\PlaceholdersExtension\Config;
3
4
use Symfony\Component\Yaml\Yaml;
5
use Ciandt\Behat\PlaceholdersExtension\Utils\PlaceholderUtils;
6
use Ciandt\Behat\PlaceholdersExtension\PlaceholderContainer\PlaceholderContainer;
7
use Ciandt\Behat\PlaceholdersExtension\Exception\MissingSectionException;
8
use Ciandt\Behat\PlaceholdersExtension\Exception\UndefinedPlaceholderException;
9
10
/**
11
 * Description of ConfigsRepository
12
 *
13
 * @author bwowk
14
 */
15
class PlaceholdersRepository
16
{
17
18
    private $configs;
19
20
    /**
21
     * @var string
22
     */
23
    private $environment;
24
25
    public function __construct($configs_mapping)
26
    {
27
        $this->configs = $this->loadConfigFiles($configs_mapping);
28
    }
29
30
    /**
31
     *
32
     * @return string[]
33
     * @todo read configs and also bring alternative @config:section tags
34
     */
35
    public function getTags()
36
    {
37
        return array_keys($this->configs);
38
    }
39
40
    /**
41
     *
42
     * @param type $config_files
0 ignored issues
show
Bug introduced by
There is no parameter named $config_files. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
43
     * @todo user %paths.base% value
44
     */
45
    private function loadConfigFiles($configs_mapping)
46
    {
47
        $placeholder_maps = array();
48
        foreach ($configs_mapping as $tag => $file_path) {
49
            $placeholder_maps[$tag]['config'] = Yaml::parse(file_get_contents($file_path));
50
            $placeholder_maps[$tag]['path'] = $file_path;
51
        }
52
        return $placeholder_maps;
53
    }
54
55 View Code Duplication
    public function getConfig($key)
0 ignored issues
show
Duplication introduced by
This method 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...
56
    {
57
        if (key_exists($key, $this->configs)) {
58
            return $this->configs[$key]['config'];
59
        }
60
        return null;
61
    }
62
63 View Code Duplication
    public function getFilePath($key)
0 ignored issues
show
Duplication introduced by
This method 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...
64
    {
65
         if (key_exists($key, $this->configs)) {
66
            return $this->configs[$key]['path'];
67
        }
68
        return null;
69
    }
70
       
71
    public function getEnvironment()
72
    {
73
        return $this->environment;
74
    }
75
76
    public function setEnvironment($environment)
77
    {
78
        $this->environment = $environment;
79
    }
80
    
81
    public function getReplacement($placeholder, PlaceholderContainer $container) {
82
        $placeholders = $this->getSectionPlaceholders($container);
83
        
84
        $variant = $container->getVariant();
85
        $environment = $this->getEnvironment();
86
        $configPath = $this->getFilePath($container->getConfigKey());
87
        $section = $container->getSectionKey();
88
        $keys = array('$' . $variant, '$' . $environment, $placeholder);
89
        $treePosition = "$configPath>$section>placeholders";
90
91
        return $this->recursivePlaceholderSearch($keys, $placeholders, $treePosition);
92
        
93
    }
94
    
95
    private function recursivePlaceholderSearch($keys, $values, $treePosition)
96
    {
97
        if (empty($keys) || is_string($values)) {
98
            return $values;
99
        }
100
        $key = array_pop($keys);
101
        if (key_exists($key, $values)) {
102
            return $this->recursivePlaceholderSearch($keys, $values[$key], "$treePosition>$key");
103
        } elseif (key_exists('$default', $values)) {
104
            return $this->recursivePlaceholderSearch($keys, $values['$default'], $treePosition . '>$default');
105
        } else {
106
            throw new UndefinedPlaceholderException("No placeholder is defined on $treePosition>$key");
107
        }
108
    }
109
    
110
    private function getSectionPlaceholders(PlaceholderContainer $container){
111
        $configKey = $container->getConfigKey();
112
        $config = $this->getConfig($configKey);
113
        $sectionKey = $container->getSectionKey();
114
        if (!key_exists($sectionKey, $config)){
115
            throw new MissingSectionException(
116
                    $configKey,
117
                    $this->getFilePath($configKey),
118
                    $sectionKey);
119
        }
120
        return $config[$sectionKey]['placeholders'];
121
    }
122
            
123
}
124