DirectoryPlaceholderObject   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 140
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.59%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 140
loc 140
ccs 50
cts 54
cp 0.9259
rs 9.3999
wmc 33
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 24 24 8
A dirsetPath() 12 12 4
A dirsetMode() 11 11 4
A dirsetRecursive() 11 11 3
A dirsetContext() 12 12 3
B dirCreate() 18 18 11

How to fix   Duplicated Code   

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:

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\php7\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\php7\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 6
    public function __construct($pathname = false, $recursive = true, $mode = false, $context = false, $logfile = false, 
54
        $loglevel = false, $UID = false, $username = false)
55
    {
56 6
        if (! empty($logfile) && file_exists($logfile) && ! empty($loglevel)) {
57 5
            $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 5
            $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 5
            $this->setUID($UID);
60 5
            $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
        }
62
        
63 6
        $pathname = (! empty($pathname) ? $this->makeAbsolute($pathname) : $this->path);
64
        
65 6
        $HID = md5($pathname);
66
        
67 6
        $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 6
        $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 6
        $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 6
        $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 6
        $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 6
        $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 6
    }
77
78
    /**
79
     *
80
     * {@inheritDoc}
81
     *
82
     */
83 6
    public function dirsetPath($pathname = false, $HID = false)
84
    {
85 6
        if (! empty($pathname) && is_string($pathname) && ! empty($HID)) {
86
            
87 6
            $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 6
            return true;
90
        } else {
91 1
            $this->debug(802, 'DirectoryTrait->dirsetPath');
92 1
            return false;
93
        }
94
    }
95
96
    /**
97
     *
98
     * {@inheritDoc}
99
     *
100
     */
101 6
    public function dirsetMode($mode = false, $HID = false)
102
    {
103 6
        if (! empty($HID) && ! empty($mode) && is_int($mode)) {
104 6
            $this->mode = $mode;
105 6
            return true;
106
        } else {
107
            
108 1
            $this->debug(802, 'DirectoryTrait->dirsetMode');
109 1
            return false;
110
        }
111
    }
112
113
    /**
114
     *
115
     * {@inheritDoc}
116
     *
117
     */
118 6
    public function dirsetRecursive($recursive = false, $HID = false)
119
    {
120 6
        if (! empty($HID) && is_bool($recursive)) {
121 6
            $this->recursive = $recursive;
122 6
            return true;
123
        } else {
124 1
            $this->debug(802, 'DirectoryTrait->dirsetRecursive');
125
        }
126
        
127 1
        return false;
128
    }
129
130
    /**
131
     *
132
     * {@inheritDoc}
133
     *
134
     */
135 2
    public function dirsetContext($context = false, $HID = false)
136
    {
137 2
        if (! empty($HID) && is_resource($context)) {
138 1
            $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 1
            return true;
140
        } 
141
142
        else {
143 1
            $this->debug(802, 'DirectoryTrait->dirsetContext');
144 1
            return false;
145
        }
146
    }
147
148
    /**
149
     *
150
     * {@inheritDoc}
151
     *
152
     */
153 6
    public function dirCreate($HID = false)
154
    {
155 6
        if ((! is_dir(dirname($this->path)) && $this->recursive === false) ||
156 6
             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 2
            $this->warning(502, $this->path);
158 2
            $result = false;
159
        } else {
160 5
            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 1
                $result = mkdir($this->path, octdec(str_pad($this->mode, 4, 0, STR_PAD_LEFT)), 
162 1
                    $this->recursive, $this->context);
163
            }
164 5
            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 5
                $result = mkdir($this->path, octdec(str_pad($this->mode, 4, 0, STR_PAD_LEFT)), true);
167
            }
168
        }
169 6
        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