Completed
Push — vendor/getid3 ( 49c253...dfd0b4 )
by Pauli
02:49
created

getid3_ivf   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B Analyze() 0 54 6
1
<?php
2
/////////////////////////////////////////////////////////////////
3
/// getID3() by James Heinrich <[email protected]>               //
4
//  available at https://github.com/JamesHeinrich/getID3       //
5
//            or https://www.getid3.org                        //
6
//            or http://getid3.sourceforge.net                 //
7
//  see readme.txt for more details                            //
8
/////////////////////////////////////////////////////////////////
9
//                                                             //
10
// module.audio.ivf.php                                        //
11
// module for analyzing IVF audio-video files                  //
12
// dependencies: NONE                                          //
13
//                                                            ///
14
/////////////////////////////////////////////////////////////////
15
16
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
17
	exit;
18
}
19
20
class getid3_ivf extends getid3_handler
21
{
22
	/**
23
	 * @return bool
24
	 */
25
	public function Analyze() {
26
		$info = &$this->getid3->info;
27
28
		$info['fileformat']          = 'ivf';
29
		$info['video']['dataformat'] = 'ivf';
30
31
		$this->fseek($info['avdataoffset']);
32
		$IVFheader = $this->fread(32);
33
34
		if (substr($IVFheader, 0, 4) == 'DKIF') {
35
36
			// https://wiki.multimedia.cx/index.php/IVF
37
			$info['ivf']['header']['signature']            =                              substr($IVFheader,  0, 4);
38
			$info['ivf']['header']['version']              = getid3_lib::LittleEndian2Int(substr($IVFheader,  4, 2)); // should be 0
39
			$info['ivf']['header']['headersize']           = getid3_lib::LittleEndian2Int(substr($IVFheader,  6, 2));
40
			$info['ivf']['header']['fourcc']               =                              substr($IVFheader,  8, 4);
41
			$info['ivf']['header']['resolution_x']         = getid3_lib::LittleEndian2Int(substr($IVFheader, 12, 2));
42
			$info['ivf']['header']['resolution_y']         = getid3_lib::LittleEndian2Int(substr($IVFheader, 14, 2));
43
			$info['ivf']['header']['timebase_numerator']   = getid3_lib::LittleEndian2Int(substr($IVFheader, 16, 4));
44
			$info['ivf']['header']['timebase_denominator'] = getid3_lib::LittleEndian2Int(substr($IVFheader, 20, 4));
45
			$info['ivf']['header']['frame_count']          = getid3_lib::LittleEndian2Int(substr($IVFheader, 24, 4));
46
			//$info['ivf']['header']['reserved']             =                              substr($IVFheader, 28, 4);
47
48
			$info['ivf']['header']['frame_rate'] = (float) $info['ivf']['header']['timebase_numerator'] / $info['ivf']['header']['timebase_denominator'];
49
50
			if ($info['ivf']['header']['version'] > 0) {
51
				$this->warning('Expecting IVF header version 0, found version '.$info['ivf']['header']['version'].', results may not be accurate');
52
			}
53
54
			$info['video']['resolution_x']    =         $info['ivf']['header']['resolution_x'];
55
			$info['video']['resolution_y']    =         $info['ivf']['header']['resolution_y'];
56
			$info['video']['codec']           =         $info['ivf']['header']['fourcc'];
57
58
			$info['ivf']['frame_count'] = 0;
59
			while (!$this->feof()) {
60
				if ($frameheader = $this->fread(12)) {
61
					$framesize = getid3_lib::LittleEndian2Int(substr($frameheader, 0, 4)); // size of frame in bytes (not including the 12-byte header)
62
					$timestamp = getid3_lib::LittleEndian2Int(substr($frameheader, 4, 8)); // 64-bit presentation timestamp
63
					$this->fseek($framesize, SEEK_CUR);
0 ignored issues
show
Bug introduced by
It seems like $framesize defined by \getid3_lib::LittleEndia...tr($frameheader, 0, 4)) on line 61 can also be of type double or false; however, getid3_handler::fseek() does only seem to accept integer, 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...
64
					$info['ivf']['frame_count']++;
65
				}
66
			}
67
			if ($info['ivf']['frame_count']) {
68
				$info['playtime_seconds']    = $timestamp / 100000;
0 ignored issues
show
Bug introduced by
The variable $timestamp 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...
69
				$info['video']['frame_rate'] = (float) $info['ivf']['frame_count'] / $info['playtime_seconds'];
70
			}
71
72
		} else {
73
			$this->error('Expecting "DKIF" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($IVFheader, 0, 4)).'"');
74
			return false;
75
		}
76
77
		return true;
78
	}
79
80
}
81