Completed
Branch master (5903e5)
by Marko
12:11
created

Filesystem::file()   C

Complexity

Conditions 14
Paths 40

Size

Total Lines 34
Code Lines 24

Duplication

Lines 34
Ratio 100 %

Code Coverage

Tests 26
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 24
c 1
b 0
f 0
nc 40
nop 5
dl 34
loc 34
ccs 26
cts 26
cp 1
crap 14
rs 5.0864

How to fix   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
 * 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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface HOWI3\libhowi\Filesystem...ilesystemInterface::dir of type object.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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