Kint_Parsers_FsPath::_parse()   F
last analyzed

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 68
Code Lines 51

Duplication

Lines 25
Ratio 36.76 %

Code Coverage

Tests 8
CRAP Score 557.1713

Importance

Changes 0
Metric Value
cc 30
dl 25
loc 68
ccs 8
cts 49
cp 0.1633
crap 557.1713
rs 2.8216
c 0
b 0
f 0
eloc 51
nc 845635
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kint\parsers\custom;
4
5
use kint\inc\KintParser;
6
7
/**
8
 * Class Kint_Parsers_FsPath
9
 */
10
class Kint_Parsers_FsPath extends KintParser
11
{
12
  /**
13
   * @param mixed $variable
14
   *
15
   * @return false|null
16
   */
17 1
  protected function _parse(&$variable)
18
  {
19
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
20
    if (
21 1
        is_object($variable)
22 1
        || is_array($variable)
23 1
        || (string)$variable !== $variable
24 1
        || strlen($variable) > 2048
25 1
        || preg_match('[[:?<>"*|]]', $variable)
26 1
        || !@is_readable($variable) # f@#! PHP and its random warnings
27
    ) {
28 1
      return false;
29
    }
30
31
    try {
32
      $fileInfo = new \SplFileInfo($variable);
33
      $flags = array();
34
      $perms = $fileInfo->getPerms();
35
36 View Code Duplication
      if (($perms & 0xC000) === 0xC000) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
        $type = 'File socket';
38
        $flags[] = 's';
39
      } elseif (($perms & 0xA000) === 0xA000) {
40
        $type = 'File symlink';
41
        $flags[] = 'l';
42
      } elseif (($perms & 0x8000) === 0x8000) {
43
        $type = 'File';
44
        $flags[] = '-';
45
      } elseif (($perms & 0x6000) === 0x6000) {
46
        $type = 'Block special file';
47
        $flags[] = 'b';
48
      } elseif (($perms & 0x4000) === 0x4000) {
49
        $type = 'Directory';
50
        $flags[] = 'd';
51
      } elseif (($perms & 0x2000) === 0x2000) {
52
        $type = 'Character special file';
53
        $flags[] = 'c';
54
      } elseif (($perms & 0x1000) === 0x1000) {
55
        $type = 'FIFO pipe file';
56
        $flags[] = 'p';
57
      } else {
58
        $type = 'Unknown file';
59
        $flags[] = 'u';
60
      }
61
62
      // owner
63
      $flags[] = (($perms & 0x0100) ? 'r' : '-');
64
      $flags[] = (($perms & 0x0080) ? 'w' : '-');
65
      $flags[] = (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));
66
67
      // group
68
      $flags[] = (($perms & 0x0020) ? 'r' : '-');
69
      $flags[] = (($perms & 0x0010) ? 'w' : '-');
70
      $flags[] = (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));
71
72
      // world
73
      $flags[] = (($perms & 0x0004) ? 'r' : '-');
74
      $flags[] = (($perms & 0x0002) ? 'w' : '-');
75
      $flags[] = (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));
76
77
      $this->type = $type;
78
      $this->size = sprintf('%.2fK', $fileInfo->getSize() / 1024);
0 ignored issues
show
Documentation Bug introduced by
The property $size was declared of type integer, but sprintf('%.2fK', $fileInfo->getSize() / 1024) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
79
      $this->value = implode($flags);
80
81
    } catch (\Exception $e) {
82
      return false;
83
    }
84
  }
85
}
86