Filesystem::link()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 7
c 1
b 0
f 1
nc 2
nop 1
dl 10
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 9.2
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\php7;
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\php7\Objects\DirectoryPlaceholderObject;
31
use \HOWI3\libhowi\Filesystem\php7\Objects\DirectoryTreeObject;
32
use \HOWI3\libhowi\Filesystem\php7\Objects\FileObject;
33
use \HOWI3\libhowi\Filesystem\php7\Objects\InfoObject;
34
use \HOWI3\libhowi\Filesystem\php7\Objects\TmpObject;
35
use \HOWI3\libhowi\Filesystem\php7\Objects\LinkObject;
36
use \HOWI3\libhowi\Filesystem\php7\TraitForFileSystem;
37
use \HOWI3\libhowi\Filesystem\php7\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 241
    public function __construct($setCwd = false)
51
    {
52 241
        $this->debug(801);
53 241
        $this->setStatus(true);
54 241
        if (! $this->setCwd($setCwd)) {
55 1
            $append = error_get_last();
56 1
            $this->warning(500, $append['message']);
57
        }
58
        
59 241
        $this->tmp()->setTmp();
60 241
    }
61
62
    /**
63
     *
64
     * {@inheritDoc}
65
     *
66
     */
67 54
    public function dir($directory = false, $dirname = false, $recursive = true, $mode = false, $context = false)
68
    {
69 54
        if (empty($directory)) {
70 1
            return false;
71
        }
72
        // ///////////
73 53
        $this->debug(807);
74 53
        if (array_key_exists($directory, $this->dirkeys) &&
75 53
             array_key_exists($this->dirkeys[$directory], $this->dirs) &&
76 53
             is_object($this->dirs[$this->dirkeys[$directory]])) {
77 46
            $response = $this->dirs[$this->dirkeys[$directory]];
78 46
            $this->response = $this->dirs[$this->dirkeys[$directory]]->response();
79 53
        } elseif (! empty($directory) && ! empty($dirname)) {
80
            
81 53
            $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...
82 53
            $HID = md5($dir);
83 53
            $this->dirkeys[$directory] = $HID;
84 53
            $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 81 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...
85 53
                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 81 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...
86 6
                $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...
87
            
88 53
            if ($this->dirs[$HID] instanceof DirectoryPlaceholderObject) {
89 6
                $this->setStatus($this->dirs[$HID]->getStatus());
90 6
                $this->setCode($this->dirs[$HID]->getCode());
91
            }
92
            
93 53
            if ($this->dirs[$HID] instanceof DirectoryTreeObject) {
94 49
                $this->dirs[$HID]->setFileClass('\HOWI3\libhowi\Filesystem\php7\Objects\FileObject');
95 49
                $this->dirs[$HID]->setInfoClass('\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject');
96 49
                $this->dirs[$HID]->setLogFile($this->getLogFile());
97 49
                $this->dirs[$HID]->setLogLevel($this->getLogLevel());
98 49
                $this->dirs[$HID]->setUID($this->getUID());
99 49
                $this->dirs[$HID]->setUsername($this->getUsername());
100
                
101 49
                $this->response->setStatus(true);
102
            } else {
103
                
104
                /* We don't need DirectoryPlaceholderObject anymore for this directory */
105 6
                if ($this->isDir($dir)) {
0 ignored issues
show
Bug introduced by
It seems like $dir defined by $this->makeAbsolute($dir...SEPARATOR . $directory) on line 81 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...
106
                    
107 5
                    $this->dirs[$HID] = new DirectoryTreeObject($dir, DirectoryTreeObject::SKIP_DOTS);
108 5
                    $this->dirs[$HID]->setFileClass('\HOWI3\libhowi\Filesystem\php7\Objects\FileObject');
109 5
                    $this->dirs[$HID]->setInfoClass('\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject');
110 5
                    $this->dirs[$HID]->setLogFile($this->getLogFile());
111 5
                    $this->dirs[$HID]->setLogLevel($this->getLogLevel());
112 5
                    $this->dirs[$HID]->setUID($this->getUID());
113 5
                    $this->dirs[$HID]->setUsername($this->getUsername());
114
                }
115
            }
116
            
117 53
            $response = $this->dirs[$HID];
118
        }
119 53
        return $response;
0 ignored issues
show
Bug introduced by
The variable $response 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...
120
    }
121
122
    /**
123
     *
124
     * {@inheritDoc}
125
     *
126
     */
127 60
    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...
128
    {
129 60
        if (! empty($filename) && array_key_exists($filename, $this->files) &&
130 60
             is_object($this->files[$filename]))
131 57
            return $this->files[$filename];
132
        
133 60
        $this->debug(808);
134 60
        if (empty($filename)) {
135 1
            $this->notice(601);
136 1
            return false;
137
        }
138
        
139 60
        $dirname = empty($dirname) ? $this->getCwd() : $this->makeAbsolute($dirname);
140 60
        $real = $dirname . DIRECTORY_SEPARATOR . $filename;
141 60
        $real = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $real);
142
        
143 60
        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...
144 1
            $this->warning(503, $real);
145 1
            return false;
146 60
        } 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...
147 4
            $result = empty($context) || ! is_resource($context) ? file_put_contents($real, $data, $flags) : file_put_contents(
148
                $real, $data, $flags, $context);
149 4
            $result = $result !== false ? $this->info(703, $real) : $this->warning(504, $real);
150
        }
151
        
152 60
        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...
153 60
            $this->files[$filename] = new FileObject($real, 'r+');
154 60
            $this->files[$filename]->setFileClass('\HOWI3\libhowi\Filesystem\php7\Objects\FileObject');
155 60
            $this->files[$filename]->setInfoClass("\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject");
156 60
            $result = $this->files[$filename];
157
        }
158
        
159 60
        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...
160
    }
161
162
    /**
163
     *
164
     * {@inheritDoc}
165
     *
166
     */
167 33
    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...
168
    {
169 33
        if (! empty($basename) && array_key_exists($basename, $this->infos) && is_object(
170 33
            $this->infos[$basename]))
171 32
            return $this->infos[$basename];
172
        
173 33
        $this->debug(809);
174 33
        if (empty($basename)) {
175 1
            $this->notice(602);
176 1
            return false;
177
        }
178
        
179 33
        $dirname = empty($directory) ? $this->getCwd() : $this->makeAbsolute($directory);
180 33
        $real = $dirname . DIRECTORY_SEPARATOR . $basename;
181 33
        $real = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $real);
182
        
183
        
184 33
        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...
185 1
            $this->warning(504, $real);
186 1
            $result = false;
187 33
        } 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...
188 33
            $this->infos[$basename] = new InfoObject($real);
189 33
            $this->infos[$basename]->setFileClass('\HOWI3\libhowi\Filesystem\php7\Objects\FileObject');
190 33
            $this->infos[$basename]->setInfoClass("\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject");
191 33
            $result = $this->infos[$basename];
192
        }
193 33
        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...
194
    }
195
196
    /**
197
     *
198
     * {@inheritDoc}
199
     *
200
     */
201 241
    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...
202
    {
203 241
        if (array_key_exists($keyword, $this->tmp) && is_object($this->tmp[$keyword]) &&
204 241
             $this->tmp[$keyword] instanceof \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\TmpInterface) {
205 4
            return $this->tmp[$keyword];
206
        } else {
207 241
            $this->tmp[$keyword] = new TmpObject();
208 241
            return $this->tmp[$keyword];
209
        }
210
    }
211
212
    /**
213
     *
214
     * {@inheritDoc}
215
     *
216
     */
217 8
    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...
218
    {
219 8
        if (array_key_exists($keyword, $this->link) && is_object($this->link[$keyword]) &&
220 8
             $this->link[$keyword] instanceof \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\LinkInterface) {
221 5
            return $this->link[$keyword];
222
        } else {
223 8
            $this->link[$keyword] = new LinkObject();
224 8
            return $this->link[$keyword];
225
        }
226
    }
227
}
228