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

lib/Utility/Utils.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
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
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)
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
}