Failed Conditions
Push — master ( 79abf5...011d1d )
by Sander
01:34
created

Utils   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 76
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTime() 0 3 1
A getMicroTime() 0 3 1
A getNameByUid() 0 4 1
A getUserInfo() 0 12 2
A splitContent() 0 15 3
A getItemByProperty() 0 8 3
1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Utility;
25
26
class Utils {
27
    /**
28
     * Gets the unix epoch UTC timestamp
29
     * @return int
30
     */
31
    public static $maxPartSize = 2621440;
32
33
	public static function getTime() {
34
		return (new \DateTime())->getTimestamp();
35
	}
36
	/**
37
	 * @return int the current unix time in milliseconds
38
	 */
39
	public static function getMicroTime() {
40
		return microtime(true);
41
	}
42
43
	/**
44
	 * @param $uid
45
	 * @return string
46
	 */
47
	public static function getNameByUid($uid){
48
		$u = Utils::getUserInfo($uid);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
49
		return $u['display_name'];
50
	}
51
52
	public static function getUserInfo($uid){
53
		$um = \OC::$server->getUserManager();
54
		$u = $um->get($uid);
55
		if($u) {
56
			$user = [
57
				'display_name' => $u->getDisplayName(),
58
				'uid' => $uid,
59
				'avatar' => $u->getAvatarImage(32)
60
			];
61
		}
62
		return $user;
0 ignored issues
show
Bug introduced by
The variable $user 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...
63
	}
64
65
	/**
66
	 * Splits a string in parts of 5Mb
67
	 * @param $str
68
	 * @return array
69
	 */
70
	public function splitContent($str) {
71
		$maxlength = Utils::$maxPartSize; // 5 Megs (2 bytes per character)
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
72
		$count = 0;
73
		$strarray = array();
74
		while (true) {
75
			if (strlen($str) <= $maxlength) {
76
				$strarray[$count++] = $str;
77
				return $strarray;
78
			} else {
79
				$strarray[$count++] = substr($str, 0, $maxlength);
80
				$str = substr($str, $maxlength);
81
			}
82
		}
83
		return $strarray;
84
	}
85
86
	/**
87
	 * @param $key
88
	 * @param $value
89
	 * @param $array
90
	 * @return int|null|string
91
	 * @internal param $ $
92
	 */
93
	public static function getItemByProperty($key, $value, $array) {
94
		foreach ($array as $_key => $val) {
95
			if ($val[$key] === $value) {
96
				return $val;
97
			}
98
		}
99
		return null;
100
	}
101
}