DirectoryPlaceholderObject::dirCreate()   B
last analyzed

Complexity

Conditions 11
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 18
loc 18
ccs 14
cts 14
cp 1
crap 11
rs 7.1162

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 - 10:02:48
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:      DirectoryPlaceholderObject.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\Objects;
25
26
use \Directory;
27
use \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\DirectoryPlaceholderInterface;
28
use \HOWI3\libhowi\Filesystem\Commons\SharedMethodsInterface;
29
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse;
30
use \HOWI3\libhowi\Filesystem\php5\TraitForSharedMethods;
31
32 View Code Duplication
class DirectoryPlaceholderObject extends Directory implements DirectoryPlaceholderInterface, 
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...
33
    SharedMethodsInterface
34
{
35
    use TraitForResponse;
36
    use TraitForSharedMethods;
37
38
    public $path = '';
39
    
40
    public $handle;
41
    
42
    private $mode = 750;
43
44
    private $recursive = true;
45
46
    private $context = false;
47
48
    /**
49
     *
50
     * {@inheritDoc}
51
     *
52
     */
53 12
    public function __construct($pathname = false, $recursive = true, $mode = false, $context = false, $logfile = false, 
54
        $loglevel = false, $UID = false, $username = false)
55
    {
56 12
        if (! empty($logfile) && file_exists($logfile) && ! empty($loglevel)) {
57 10
            $this->setLogfile($logfile);
0 ignored issues
show
Documentation introduced by
$logfile is of type boolean, but the function expects a false|string.

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...
58 10
            $this->setLoglevel($loglevel);
0 ignored issues
show
Documentation introduced by
$loglevel is of type boolean, but the function expects a integer.

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...
59 10
            $this->setUID($UID);
60 10
            $this->setUsername($username);
0 ignored issues
show
Documentation introduced by
$username is of type boolean, but the function expects a string.

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...
61 10
        }
62
        
63 12
        $pathname = (! empty($pathname) ? $this->makeAbsolute($pathname) : $this->path);
64
        
65 12
        $HID = md5($pathname);
66
        
67 12
        $this->dirsetPath($pathname, $HID);
0 ignored issues
show
Bug introduced by
It seems like $pathname defined by !empty($pathname) ? $thi...pathname) : $this->path on line 63 can also be of type string; however, HOWI3\libhowi\Filesystem...derObject::dirsetPath() 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...
Documentation introduced by
$HID 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...
68 12
        $this->dirsetMode(! empty($mode) ? $mode : $this->mode, $HID);
0 ignored issues
show
Bug introduced by
It seems like !empty($mode) ? $mode : $this->mode can also be of type integer; however, HOWI3\libhowi\Filesystem...derObject::dirsetMode() 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...
Documentation introduced by
$HID 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...
69
        
70 12
        $this->dirsetRecursive(! empty($recursive) ? true : false, $HID);
0 ignored issues
show
Documentation introduced by
$HID 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...
71
        
72 12
        $context = ! empty($context) ? $this->dirsetContext($context, $HID) : false;
0 ignored issues
show
Documentation introduced by
$HID 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...
Unused Code introduced by
$context is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
        
74 12
        $this->info(701, $pathname);
0 ignored issues
show
Bug introduced by
It seems like $pathname defined by !empty($pathname) ? $thi...pathname) : $this->path on line 63 can also be of type boolean; however, HOWI3\libhowi\Filesystem...raitForResponse::info() does only seem to accept false|string, 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...
75 12
        $this->response()->setStatus($this->dirCreate($HID));
0 ignored issues
show
Documentation introduced by
$HID 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...
76 12
    }
77
78
    /**
79
     *
80
     * {@inheritDoc}
81
     *
82
     */
83 12
    public function dirsetPath($pathname = false, $HID = false)
84
    {
85 12
        if (! empty($pathname) && is_string($pathname) && ! empty($HID)) {
86
            
87 12
            $this->path = $this->makeAbsolute($pathname);
0 ignored issues
show
Documentation introduced by
$pathname 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 Bug introduced by
It seems like $this->makeAbsolute($pathname) can also be of type boolean. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
88
            
89 12
            return true;
90
        } else {
91 2
            $this->debug(802, 'DirectoryTrait->dirsetPath');
92 2
            return false;
93
        }
94
    }
95
96
    /**
97
     *
98
     * {@inheritDoc}
99
     *
100
     */
101 12
    public function dirsetMode($mode = false, $HID = false)
102
    {
103 12
        if (! empty($HID) && ! empty($mode) && is_int($mode)) {
104 12
            $this->mode = $mode;
105 12
            return true;
106
        } else {
107
            
108 2
            $this->debug(802, 'DirectoryTrait->dirsetMode');
109 2
            return false;
110
        }
111
    }
112
113
    /**
114
     *
115
     * {@inheritDoc}
116
     *
117
     */
118 12
    public function dirsetRecursive($recursive = false, $HID = false)
119
    {
120 12
        if (! empty($HID) && is_bool($recursive)) {
121 12
            $this->recursive = $recursive;
122 12
            return true;
123
        } else {
124 2
            $this->debug(802, 'DirectoryTrait->dirsetRecursive');
125
        }
126
        
127 2
        return false;
128
    }
129
130
    /**
131
     *
132
     * {@inheritDoc}
133
     *
134
     */
135 4
    public function dirsetContext($context = false, $HID = false)
136
    {
137 4
        if (! empty($HID) && is_resource($context)) {
138 2
            $this->context = $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $context of type resource is incompatible with the declared type boolean of property $context.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
139 2
            return true;
140
        } 
141
142
        else {
143 2
            $this->debug(802, 'DirectoryTrait->dirsetContext');
144 2
            return false;
145
        }
146
    }
147
148
    /**
149
     *
150
     * {@inheritDoc}
151
     *
152
     */
153 12
    public function dirCreate($HID = false)
154
    {
155 12
        if ((! is_dir(dirname($this->path)) && $this->recursive === false) ||
156 12
             is_dir(dirname($this->path)) && ! $this->isWritable(dirname($this->path))) {
0 ignored issues
show
Documentation introduced by
dirname($this->path) 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...
157 4
            $this->warning(502, $this->path);
158 4
            $result = false;
159 4
        } else {
160 10
            if (! empty($HID) && ! $this->exists($this->path) && ! empty($this->context)) {
0 ignored issues
show
Documentation introduced by
$this->path 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...
161 2
                $result = mkdir($this->path, octdec(str_pad($this->mode, 4, 0, STR_PAD_LEFT)), 
162 2
                    $this->recursive, $this->context);
163 2
            }
164 10
            if (! empty($HID) && ! $this->exists($this->path) && empty($this->context)) {
0 ignored issues
show
Documentation introduced by
$this->path 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...
165
                
166 10
                $result = mkdir($this->path, octdec(str_pad($this->mode, 4, 0, STR_PAD_LEFT)), true);
167 10
            }
168
        }
169 12
        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...
170
    }
171
}
172