TmpObject::getTmp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 0
dl 9
loc 9
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.6666
1
<?php
2
/*******************************************************************
3
 * Created by:  Marko Kungla @ OkramLabs on aug 6, 2012 - 9:11:06
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:      TmpTrait.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 HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\TmpInterface;
27
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse;
28
use \HOWI3\libhowi\Filesystem\php7\TraitForSharedMethods;
29
30 View Code Duplication
class TmpObject implements TmpInterface
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...
31
{
32
    
33
    use TraitForResponse;
34
    use TraitForSharedMethods;
35
36
    private $tmp_path;
37
38
    /**
39
     *
40
     * {@inheritDoc}
41
     *
42
     */
43 241
    public function setTmp($path = 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...
44
    {
45 241
        if (! empty($path)) {
46 3
            $tmp = $this->makeAbsolute($path);
47 3
            $isWritable = $this->isWritable($tmp);
0 ignored issues
show
Bug introduced by
It seems like $tmp defined by $this->makeAbsolute($path) on line 46 can also be of type string; however, HOWI3\libhowi\Filesystem...edMethods::isWritable() 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...
48 3
            $this->tmp_path = $isWritable ? $tmp : $this->tmp_path;
49
            
50 3
            ! empty($isWritable) ? $this->debug(803) : $this->warning(501, $tmp);
0 ignored issues
show
Bug introduced by
It seems like $tmp defined by $this->makeAbsolute($path) on line 46 can also be of type boolean; however, HOWI3\libhowi\Filesystem...tForResponse::warning() 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...
51 3
            $this->response()->setStatus($isWritable);
52
        } else {
53 241
            $this->tmp_path = sys_get_temp_dir();
54
            
55 241
            $this->debug(804);
56 241
            $this->response()->setStatus(true);
57 241
            $isWritable = true;
58
        }
59 241
        return $isWritable;
60
    }
61
62
    /**
63
     *
64
     * {@inheritDoc}
65
     *
66
     */
67 2
    public function getTmp()
68
    {
69 2
        if (empty($this->tmp_path))
70
            $this->tmp_path = $this->setTmp();
71
        
72 2
        $this->debug(805, $this->tmp_path);
73 2
        $this->response()->setStatus(true);
74 2
        return $this->tmp_path;
75
    }
76
77
    /**
78
     *
79
     * {@inheritDoc}
80
     *
81
     */
82 1
    public function tempnam($dir = false, $prefix = 'howi-fs-tmp-')
83
    {
84 1
        $dir = empty($dir) ? $this->getTmp() : $this->makeAbsolute($dir);
85
        
86 1
        $tmpname = tempnam($dir, $prefix);
87
        
88 1
        $this->debug(806, $tmpname);
89 1
        $this->response()->setStatus(true);
90
        
91 1
        return $tmpname;
92
    }
93
94
    /**
95
     *
96
     * {@inheritDoc}
97
     *
98
     */
99 1
    public function tmpfile()
100
    {
101 1
        $this->debug(807);
102 1
        $tmpfie = tmpfile();
103 1
        $this->response()->setStatus(! empty($tmpfie) ? true : false);
104 1
        return $tmpfie;
105
    }
106
}
107