Filesystem   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 191
Duplicated Lines 100 %

Coupling/Cohesion

Components 3
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 191
loc 191
ccs 110
cts 110
cp 1
rs 8.3396
wmc 44
lcom 3
cbo 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
C dir() 56 56 11
C file() 34 34 14
D infoObject() 28 28 9
A tmp() 10 10 4
A link() 10 10 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Filesystem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Filesystem, and based on these observations, apply Extract Interface, too.

1
<?php
2
/********************************************************************
3
 * Created by:	Marko Kungla @ OkramLabs on Aug 6, 2012 - 9:21:34
4
 * Contact:		[email protected] - https://okramlabs.com
5
 * @copyright:	2015 OkramLabs - https://okramlabs.com
6
 * @license		MIT
7
 *
8
 * Package name:libhowi-filesystem
9
 * @category	HOWI3
10
 * @package		libhowi
11
 * @subpackage	filesystem
12
 * 
13
 * Lang:		PHP
14
 * Encoding:	UTF-8
15
 * File:		Filesystem.inc
16
 * @link		https://
17
 ********************************************************************
18
 * Contributors:
19
 * @author		Marko Kungla <[email protected]>
20
 *  	Github:	https://github.com/mkungla
21
 ********************************************************************
22
 * Comments:
23
 */
24
namespace HOWI3\libhowi\Filesystem\php5;
25
26
use \HOWI3\libhowi\Filesystem\Commons\AbstractFilesystem;
27
use \HOWI3\libhowi\Filesystem\Commons\FilesystemInterface;
28
use \HOWI3\libhowi\Filesystem\Commons\SharedMethodsInterface;
29
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse;
30
use \HOWI3\libhowi\Filesystem\php5\Objects\DirectoryPlaceholderObject;
31
use \HOWI3\libhowi\Filesystem\php5\Objects\DirectoryTreeObject;
32
use \HOWI3\libhowi\Filesystem\php5\Objects\FileObject;
33
use \HOWI3\libhowi\Filesystem\php5\Objects\InfoObject;
34
use \HOWI3\libhowi\Filesystem\php5\Objects\TmpObject;
35
use \HOWI3\libhowi\Filesystem\php5\Objects\LinkObject;
36
use \HOWI3\libhowi\Filesystem\php5\TraitForFileSystem;
37
use \HOWI3\libhowi\Filesystem\php5\TraitForSharedMethods;
38
39 View Code Duplication
class Filesystem extends AbstractFilesystem implements FilesystemInterface, SharedMethodsInterface
0 ignored issues
show
Duplication introduced by
This class 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...
40
{
41
    use TraitForResponse;
42
    use TraitForFileSystem;
43
    use TraitForSharedMethods;
44
45
    /**
46
     *
47
     * {@inheritDoc}
48
     *
49
     */
50 490
    public function __construct($setCwd = false)
51
    {
52 490
        $this->debug(801);
53 490
        $this->setStatus(true);
54 490
        if (! $this->setCwd($setCwd)) {
55 2
            $append = error_get_last();
56 2
            $this->warning(500, $append['message']);
57 2
        }
58
        
59 490
        $this->tmp()->setTmp();
60 490
    }
61
62
    /**
63
     *
64
     * {@inheritDoc}
65
     *
66
     */
67 110
    public function dir($directory = false, $dirname = false, $recursive = true, $mode = false, $context = false)
68
    {
69 110
        $response = null;
70 110
        if (empty($directory)) {
71 2
            return false;
72
        }
73
        // ///////////
74 108
        $this->debug(807);
75 108
        if (array_key_exists($directory, $this->dirkeys) &&
76 108
             array_key_exists($this->dirkeys[$directory], $this->dirs) &&
77 108
             is_object($this->dirs[$this->dirkeys[$directory]])) {
78 92
            $response = $this->dirs[$this->dirkeys[$directory]];
79 92
            $this->response = $this->dirs[$this->dirkeys[$directory]]->response();
80 108
        } elseif (! empty($directory) && ! empty($dirname)) {
81
            
82 108
            $dir = $this->makeAbsolute($dirname . DIRECTORY_SEPARATOR . $directory);
0 ignored issues
show
Documentation introduced by
$dirname . DIRECTORY_SEPARATOR . $directory is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83 108
            $HID = md5($dir);
84 108
            $this->dirkeys[$directory] = $HID;
85 108
            $this->dirs[$HID] = $this->isDir($dir) ? new DirectoryTreeObject($dir, 
0 ignored issues
show
Bug introduced by
It seems like $dir defined by $this->makeAbsolute($dir...SEPARATOR . $directory) on line 82 can also be of type string; however, HOWI3\libhowi\Filesystem...rSharedMethods::isDir() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86 108
                DirectoryTreeObject::SKIP_DOTS) : new DirectoryPlaceholderObject($dir, $recursive, $mode, 
0 ignored issues
show
Bug introduced by
It seems like $dir defined by $this->makeAbsolute($dir...SEPARATOR . $directory) on line 82 can also be of type string; however, HOWI3\libhowi\Filesystem...erObject::__construct() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87 12
                $context, $this->getLogFile(), $this->getLogLevel(), $this->getUID(), $this->getUsername());
0 ignored issues
show
Documentation introduced by
$this->getLogFile() is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->getLogLevel() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->getUID() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
            
89 108
            if ($this->dirs[$HID] instanceof DirectoryPlaceholderObject) {
90 12
                $this->setStatus($this->dirs[$HID]->getStatus());
91 12
                $this->setCode($this->dirs[$HID]->getCode());
92 12
            }
93
            
94 108
            if ($this->dirs[$HID] instanceof DirectoryTreeObject) {
95 98
                $this->dirs[$HID]->setFileClass('\HOWI3\libhowi\Filesystem\php5\Objects\FileObject');
96 98
                $this->dirs[$HID]->setInfoClass('\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject');
97 98
                $this->dirs[$HID]->setLogFile($this->getLogFile());
98 98
                $this->dirs[$HID]->setLogLevel($this->getLogLevel());
99 98
                $this->dirs[$HID]->setUID($this->getUID());
100 98
                $this->dirs[$HID]->setUsername($this->getUsername());
101
                
102 98
                $this->response->setStatus(true);
103 98
            } else {
104
                
105
                /* We don't need DirectoryPlaceholderObject anymore for this directory */
106 12
                if ($this->isDir($dir)) {
0 ignored issues
show
Bug introduced by
It seems like $dir defined by $this->makeAbsolute($dir...SEPARATOR . $directory) on line 82 can also be of type string; however, HOWI3\libhowi\Filesystem...rSharedMethods::isDir() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
                    
108 10
                    $this->dirs[$HID] = new DirectoryTreeObject($dir, DirectoryTreeObject::SKIP_DOTS);
109 10
                    $this->dirs[$HID]->setFileClass('\HOWI3\libhowi\Filesystem\php5\Objects\FileObject');
110 10
                    $this->dirs[$HID]->setInfoClass('\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject');
111 10
                    $this->dirs[$HID]->setLogFile($this->getLogFile());
112 10
                    $this->dirs[$HID]->setLogLevel($this->getLogLevel());
113 10
                    $this->dirs[$HID]->setUID($this->getUID());
114 10
                    $this->dirs[$HID]->setUsername($this->getUsername());
115 10
                }
116
            }
117
            
118 108
            $response = $this->dirs[$HID];
119 108
        }
120
        
121 108
        return $response;
122
    }
123
124
    /**
125
     *
126
     * {@inheritDoc}
127
     *
128
     */
129 120
    public function file($filename = false, $dirname = false, $data = '', $flags = FILE_APPEND, $context = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
130
    {
131 120
        if (! empty($filename) && array_key_exists($filename, $this->files) &&
132 114
             is_object($this->files[$filename]))
133 120
            return $this->files[$filename];
134
        
135 120
        $this->debug(808);
136 120
        if (empty($filename)) {
137 2
            $this->notice(601);
138 2
            return false;
139
        }
140
        
141 120
        $dirname = empty($dirname) ? $this->getCwd() : $this->makeAbsolute($dirname);
142 120
        $real = $dirname . DIRECTORY_SEPARATOR . $filename;
143 120
        $real = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $real);
144
        
145 120
        if (! $this->exists($real) && ! $this->isWritable(dirname($real))) {
0 ignored issues
show
Documentation introduced by
$real is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
dirname($real) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146 2
            $this->warning(503, $real);
147 2
            return false;
148 120
        } elseif (! $this->exists($real) && $this->isWritable(dirname($real))) {
0 ignored issues
show
Documentation introduced by
$real is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
dirname($real) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149 8
            $result = empty($context) || ! is_resource($context) ? file_put_contents($real, $data, $flags) : file_put_contents(
150 8
                $real, $data, $flags, $context);
151 8
            $result = $result !== false ? $this->info(703, $real) : $this->warning(504, $real);
152 8
        }
153
        
154 120
        if ($this->exists($real)) {
0 ignored issues
show
Documentation introduced by
$real is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155 120
            $this->files[$filename] = new FileObject($real, 'r+');
156 120
            $this->files[$filename]->setFileClass('\HOWI3\libhowi\Filesystem\php5\Objects\FileObject');
157 120
            $this->files[$filename]->setInfoClass("\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject");
158 120
            $result = $this->files[$filename];
159 120
        }
160
        
161 120
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
162
    }
163
164
    /**
165
     *
166
     * {@inheritDoc}
167
     *
168
     */
169 66
    public function infoObject($basename = false, $directory = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
170
    {
171 66
        if (! empty($basename) && array_key_exists($basename, $this->infos) && is_object(
172 64
            $this->infos[$basename]))
173 66
            return $this->infos[$basename];
174
        
175 66
        $this->debug(809);
176 66
        if (empty($basename)) {
177 2
            $this->notice(602);
178 2
            return false;
179
        }
180
        
181 66
        $dirname = empty($directory) ? $this->getCwd() : $this->makeAbsolute($directory);
182 66
        $real = $dirname . DIRECTORY_SEPARATOR . $basename;
183 66
        $real = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $real);
184
        
185
        
186 66
        if (! $this->exists($real) && ! $this->isReadable($real)) {
0 ignored issues
show
Documentation introduced by
$real is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187 2
            $this->warning(504, $real);
188 2
            $result = false;
189 66
        } elseif ($this->exists($real)) {
0 ignored issues
show
Documentation introduced by
$real is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
190 66
            $this->infos[$basename] = new InfoObject($real);
191 66
            $this->infos[$basename]->setFileClass('\HOWI3\libhowi\Filesystem\php5\Objects\FileObject');
192 66
            $this->infos[$basename]->setInfoClass("\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject");
193 66
            $result = $this->infos[$basename];
194 66
        }
195 66
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
196
    }
197
198
    /**
199
     *
200
     * {@inheritDoc}
201
     *
202
     */
203 490
    public function tmp($keyword = 'tmp')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
204
    {
205 490
        if (array_key_exists($keyword, $this->tmp) && is_object($this->tmp[$keyword]) &&
206 490
             $this->tmp[$keyword] instanceof \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\TmpInterface) {
207 16
            return $this->tmp[$keyword];
208
        } else {
209 490
            $this->tmp[$keyword] = new TmpObject();
210 490
            return $this->tmp[$keyword];
211
        }
212
    }
213
214
    /**
215
     *
216
     * {@inheritDoc}
217
     *
218
     */
219 16
    public function link($keyword = 'link')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
220
    {
221 16
        if (array_key_exists($keyword, $this->link) && is_object($this->link[$keyword]) &&
222 16
             $this->link[$keyword] instanceof \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\LinkInterface) {
223 10
            return $this->link[$keyword];
224
        } else {
225 16
            $this->link[$keyword] = new LinkObject();
226 16
            return $this->link[$keyword];
227
        }
228
    }
229
}
230