Completed
Push — master ( d35b5f...2bf577 )
by Lukas
05:59 queued 05:51
created

Helper::parseFileId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 19
cp 0
rs 9.2
cc 4
eloc 16
nc 4
nop 1
crap 20
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments;
13
14
class Helper {
15
	const APP_ID = 'richdocuments';
16
17
	/**
18
	 * @param string $fileId
19
	 * @return array
20
	 * @throws \Exception
21
	 */
22
	public static function parseFileId($fileId) {
23
		$arr = explode('_', $fileId);
24
		if (count($arr) === 1) {
25
			$fileId = $arr[0];
26
			$version = '0';
27
		} else if (count($arr) === 2) {
28
			list($fileId, $instanceId) = $arr;
29
			$version = '0';
30
		} else if (count($arr) === 3) {
31
			list($fileId, $instanceId, $version) = $arr;
32
		} else {
33
			throw new \Exception('$fileId has not the expected format');
34
		}
35
36
		return [
37
			$fileId,
38
			$instanceId,
0 ignored issues
show
Bug introduced by
The variable $instanceId 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...
39
			$version,
40
		];
41
	}
42
43
44
	public static function getNewFileName($view, $path, $prepend = ' '){
45
		$fileNum = 1;
46
47
		while ($view->file_exists($path)){
48
			$fileNum += 1;
49
			$path = preg_replace('/(\.|' . $prepend . '\(\d+\)\.)([^.]*)$/', $prepend . '(' . $fileNum . ').$2', $path);
50
		};
51
52
		return $path;
53
	}
54
}
55