Kint_Object_Representation_Source::getSource()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 3
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Kint_Object_Representation_Source extends Kint_Object_Representation
0 ignored issues
show
Coding Style introduced by
Kint_Object_Representation_Source does not seem to conform to the naming convention (^[A-Z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
4
{
5
    public $name = 'source';
6
    public $label = 'Source';
7
    public $hints = array('source');
8
    public $source = array();
9
    public $filename = null;
10
    public $line = 0;
11
12
    public function __construct($filename, $line, $padding = 7)
13
    {
14
        $this->filename = $filename;
15
        $this->line = $line;
16
17
        $start_line = max($line - $padding, 1);
0 ignored issues
show
Coding Style introduced by
$start_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
        $length = $line + $padding + 1 - $start_line;
0 ignored issues
show
Coding Style introduced by
$start_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
        $this->source = self::getSource($filename, $start_line, $length);
0 ignored issues
show
Coding Style introduced by
$start_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation Bug introduced by
It seems like self::getSource($filename, $start_line, $length) can also be of type false. However, the property $source is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
20
        if ($this->source !== false) {
21
            $this->contents = implode("\n", $this->source);
0 ignored issues
show
Documentation Bug introduced by
It seems like implode(' ', $this->source) of type string is incompatible with the declared type array of property $contents.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22
        }
23
    }
24
25
    /**
26
     * Gets section of source code.
27
     *
28
     * @param string   $filename   Full path to file
29
     * @param int      $start_line The first line to display (1 based)
30
     * @param int|null $length     Amount of lines to show
31
     *
32
     * @return bool|array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
33
     */
34
    public static function getSource($filename, $start_line = 1, $length = null)
0 ignored issues
show
Coding Style introduced by
$start_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The parameter $start_line is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
35
    {
36
        if (!$filename or !is_readable($filename)) {
37
            return false;
38
        }
39
        $source = preg_split("/\r\n|\n|\r/", file_get_contents($filename));
40
        $source = array_combine(range(1, count($source)), $source);
41
        $source = array_slice($source, $start_line - 1, $length, true);
0 ignored issues
show
Coding Style introduced by
$start_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
42
43
        return $source;
44
    }
45
}
46