Kint_Parsers_FsPath   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 76
Duplicated Lines 32.89 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 16.33%

Importance

Changes 0
Metric Value
dl 25
loc 76
ccs 8
cts 49
cp 0.1633
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
F _parse() 25 68 30

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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