getIncludePath()   F
last analyzed

Complexity

Conditions 23
Paths 198

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
nc 198
nop 3
dl 0
loc 61
rs 3.35
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Smarty read include path plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsInternal
7
 * @author     Monte Ohrt
8
 */
9
10
/**
11
 * Smarty Internal Read Include Path Class
12
 *
13
 * @package    Smarty
14
 * @subpackage PluginsInternal
15
 */
16
class Smarty_Internal_Runtime_GetIncludePath
17
{
18
    /**
19
     * include path cache
20
     *
21
     * @var string
22
     */
23
    public $_include_path = '';
24
25
    /**
26
     * include path directory cache
27
     *
28
     * @var array
29
     */
30
    public $_include_dirs = array();
31
32
    /**
33
     * include path directory cache
34
     *
35
     * @var array
36
     */
37
    public $_user_dirs = array();
38
39
    /**
40
     * stream cache
41
     *
42
     * @var string[][]
43
     */
44
    public $isFile = array();
45
46
    /**
47
     * stream cache
48
     *
49
     * @var string[]
50
     */
51
    public $isPath = array();
52
53
    /**
54
     * stream cache
55
     *
56
     * @var int[]
57
     */
58
    public $number = array();
59
60
    /**
61
     * status cache
62
     *
63
     * @var bool
64
     */
65
    public $_has_stream_include = null;
66
67
    /**
68
     * Number for array index
69
     *
70
     * @var int
71
     */
72
    public $counter = 0;
73
74
    /**
75
     * Check if include path was updated
76
     *
77
     * @param \Smarty $smarty
78
     *
79
     * @return bool
80
     */
81
    public function isNewIncludePath(Smarty $smarty)
82
    {
83
        $_i_path = get_include_path();
84
        if ($this->_include_path !== $_i_path) {
85
            $this->_include_dirs = array();
86
            $this->_include_path = $_i_path;
87
            $_dirs = (array)explode(PATH_SEPARATOR, $_i_path);
88
            foreach ($_dirs as $_path) {
89
                if (is_dir($_path)) {
90
                    $this->_include_dirs[] = $smarty->_realpath($_path . DIRECTORY_SEPARATOR, true);
91
                }
92
            }
93
            return true;
94
        }
95
        return false;
96
    }
97
98
    /**
99
     * return array with include path directories
100
     *
101
     * @param \Smarty $smarty
102
     *
103
     * @return array
104
     */
105
    public function getIncludePathDirs(Smarty $smarty)
106
    {
107
        $this->isNewIncludePath($smarty);
108
        return $this->_include_dirs;
109
    }
110
111
    /**
112
     * Return full file path from PHP include_path
113
     *
114
     * @param string[] $dirs
115
     * @param string   $file
116
     * @param \Smarty  $smarty
117
     *
118
     * @return bool|string full filepath or false
119
     */
120
    public function getIncludePath($dirs, $file, Smarty $smarty)
121
    {
122
        //if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : $this->_has_stream_include = false)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
        if (!(isset($this->_has_stream_include) ? $this->_has_stream_include :
124
            $this->_has_stream_include = function_exists('stream_resolve_include_path'))
125
        ) {
126
            $this->isNewIncludePath($smarty);
127
        }
128
        // try PHP include_path
129
        foreach ($dirs as $dir) {
130
            $dir_n = isset($this->number[ $dir ]) ? $this->number[ $dir ] : $this->number[ $dir ] = $this->counter++;
131
            if (isset($this->isFile[ $dir_n ][ $file ])) {
132
                if ($this->isFile[ $dir_n ][ $file ]) {
133
                    return $this->isFile[ $dir_n ][ $file ];
134
                } else {
135
                    continue;
136
                }
137
            }
138
            if (isset($this->_user_dirs[ $dir_n ])) {
139
                if (false === $this->_user_dirs[ $dir_n ]) {
140
                    continue;
141
                } else {
142
                    $dir = $this->_user_dirs[ $dir_n ];
143
                }
144
            } else {
145
                if ($dir[ 0 ] === '/' || $dir[ 1 ] === ':') {
146
                    $dir = str_ireplace(getcwd(), '.', $dir);
147
                    if ($dir[ 0 ] === '/' || $dir[ 1 ] === ':') {
148
                        $this->_user_dirs[ $dir_n ] = false;
149
                        continue;
150
                    }
151
                }
152
                $dir = substr($dir, 2);
153
                $this->_user_dirs[ $dir_n ] = $dir;
154
            }
155
            if ($this->_has_stream_include) {
156
                $path = stream_resolve_include_path($dir . (isset($file) ? $file : ''));
157
                if ($path) {
158
                    return $this->isFile[ $dir_n ][ $file ] = $path;
159
                }
160
            } else {
161
                foreach ($this->_include_dirs as $key => $_i_path) {
162
                    $path = isset($this->isPath[ $key ][ $dir_n ]) ? $this->isPath[ $key ][ $dir_n ] :
163
                        $this->isPath[ $key ][ $dir_n ] = is_dir($_dir_path = $_i_path . $dir) ? $_dir_path : false;
164
                    if ($path === false) {
165
                        continue;
166
                    }
167
                    if (isset($file)) {
168
                        $_file = $this->isFile[ $dir_n ][ $file ] = (is_file($path . $file)) ? $path . $file : false;
169
                        if ($_file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $_file of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
170
                            return $_file;
171
                        }
172
                    } else {
173
                        // no file was given return directory path
174
                        return $path;
175
                    }
176
                }
177
            }
178
        }
179
        return false;
180
    }
181
}
182