GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PHPFile::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace UCD\Infrastructure\Repository\CharacterRepository\FileRepository\File;
4
5
use PhpParser\Node\Expr\Array_;
6
use PhpParser\Node\Expr\ArrayItem;
7
use PhpParser\Node\Scalar\LNumber;
8
use PhpParser\Node\Scalar\String_;
9
use PhpParser\Node\Stmt\Return_;
10
use PhpParser\PrettyPrinter\Standard;
11
12
use UCD\Exception\UnexpectedValueException;
13
use UCD\Infrastructure\Repository\CharacterRepository\FileRepository\File;
14
15
class PHPFile implements File
16
{
17
    /**
18
     * @var \SplFileInfo
19
     */
20
    private $fileInfo;
21
22
    /**
23
     * @param \SplFileInfo $fileInfo
24
     */
25
    public function __construct(\SplFileInfo $fileInfo)
26
    {
27
        $this->fileInfo = $fileInfo;
28
    }
29
30
    /**
31
     * @return string[]
32
     * @throws UnexpectedValueException
33
     */
34
    public function readArray()
35
    {
36
        $value = $this->readFile();
37
        $this->verifyType($value, 'array');
38
39
        return $value;
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    private function readFile()
46
    {
47
        $pathname = $this->fileInfo->getPathname();
48
49
        return require $this->isZippedFile() ? 'compress.zlib://' . $pathname : $pathname;
50
    }
51
52
    /**
53
     * @param string $value
54
     * @param string $expectedType
55
     * @throws UnexpectedValueException
56
     */
57
    private function verifyType($value, $expectedType)
58
    {
59
        $actualType = gettype($value);
60
61
        if ($actualType !== $expectedType) {
62
            throw new UnexpectedValueException(sprintf('Expected %s, got %s', $expectedType, $actualType));
63
        }
64
    }
65
66
    /**
67
     * @param string[] $items
68
     * @return bool
69
     */
70
    public function writeArray(array $items)
71
    {
72
        $content = $this->generateFileContentForArray($items);
73
74
        $fileInfo = $this->isZippedFile()
75
            ? new \SplFileInfo('compress.zlib://' . $this->fileInfo->getPathname())
76
            : $this->fileInfo;
77
        $file = $fileInfo->openFile('w');
78
        $file->fwrite($content);
79
80
        return $file->fflush();
81
    }
82
83
    /**
84
     * @param array $items
85
     * @return string
86
     */
87
    private function generateFileContentForArray(array $items)
88
    {
89
        $nodes = [];
90
91
        foreach ($items as $key => $value) {
92
            $key = is_int($key) ? new LNumber($key) : new String_($key);
93
            $value = new String_($value);
94
            $node = new ArrayItem($value, $key);
95
            array_push($nodes, $node);
96
        }
97
98
        $statements = [new Return_(new Array_($nodes))];
99
        $printer = new Standard();
100
101
        return $printer->prettyPrintFile($statements);
102
    }
103
104
    /**
105
     * @return \SplFileInfo
106
     */
107
    public function getInfo()
108
    {
109
        return $this->fileInfo;
110
    }
111
112
    /**
113
     * @param array $items
0 ignored issues
show
Bug introduced by
There is no parameter named $items. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
114
     * @return boolean
115
     */
116
    private function isZippedFile()
117
    {
118
        if (preg_match('`^phpvfs[0-9a-z]*://`i', $this->fileInfo->getPathname())) {
119
            // php-vfs does not support combining stream wrappers
120
            return false;
121
        }
122
123
        return $this->fileInfo->getExtension() === 'gz';
124
    }
125
}
126