Skip to content

Commit 4368e3a

Browse files
committed
Fix based on static analysis
1 parent 1ee2cce commit 4368e3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+712
-444
lines changed

.github/workflows/test.yml

+7-12
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@ on:
99
- master
1010

1111
jobs:
12-
phpcs:
13-
runs-on: ubuntu-latest
14-
steps:
15-
- uses: actions/checkout@v1
16-
- name: PHP Code Scanner Scan
17-
uses: rtCamp/action-phpcs-code-review@master
18-
continue-on-error: true
19-
env:
20-
GH_BOT_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21-
with:
22-
args: "PSR12"
2312
test:
2413
runs-on: ubuntu-latest
2514
name: PHP ${{ matrix.php-versions }}
@@ -36,7 +25,7 @@ jobs:
3625
php-version: ${{ matrix.php-versions }}
3726
extensions: curl,json,mbstring,uopz
3827
coverage: pcov
39-
tools: pecl
28+
tools: pecl,phpstan,phpunit,cs2pr,phpcs
4029

4130
- name: Get Composer Cache Directory
4231
id: composer-cache
@@ -61,6 +50,12 @@ jobs:
6150
- name: Setup Problem Matchers for PHPUnit
6251
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
6352

53+
- name: PHPStan
54+
run: phpstan analyse --level=3 --error-format=checkstyle src/ | cs2pr
55+
56+
- name: PHPCS
57+
run: phpcs --standard=PSR12 --ignore=\*/Tests/\*,\*Minifier.php --exclude=PSR1.Methods.CamelCapsMethodName,Generic.Files.LineLength src/ | cs2pr
58+
6459
- name: Upload coverage result
6560
uses: actions/upload-artifact@v1
6661
with:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.idea/
22
/.phpintel/
3+
/tests/.phpunit.result.cache
34

45
# IntelliJ
56
/build/coverage

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"lunr/halo": "dev-master",
3131
"phpunit/phpunit": "~8.0",
3232
"theseer/autoload": "~1.0",
33-
"phing/phing": "~2.0"
33+
"phing/phing": "~2.0",
34+
"phpstan/phpstan-phpunit": "^0.12.6",
35+
"phpstan/phpstan": "^0.12.8"
3436
},
3537
"scripts": {
3638
"test": [

composer.lock

+96-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpdraft

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use PHPDraft\In\ApibFileParser;
1414
use PHPDraft\Out\Sorting;
1515
use PHPDraft\Out\Version;
1616
use PHPDraft\Parse\ExecutionException;
17-
use PHPDraft\Parse\JsonToHTML;
18-
use PHPDraft\Parse\LegacyHtmlGenerator;
1917
use PHPDraft\Parse\ParserFactory;
2018
use PHPDraft\Parse\ResourceException;
2119

phpstan.neon

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
bootstrap: tests/test.bootstrap.inc.php
3+
ignoreErrors:
4+
- '#Access to an undefined property object::\$[a-zA-Z0-9\\_]+#'
5+
- '#Access to an undefined property PHPUnit\\Framework\\MockObject\\MockObject::\$[a-zA-Z0-9_]+#'
6+
- '#Access to an undefined property PHPDraft\\Model\\HierarchyElement::\$[a-zA-Z0-9_]+#'

src/PHPDraft/Core/Autoloader.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file contains the Autoloader.
45
*
@@ -15,7 +16,7 @@ function ($classname) {
1516
$classname = ltrim($classname, '\\');
1617
preg_match('/^(.+)?([^\\\\]+)$/U', $classname, $match);
1718
$classname = str_replace('\\', '/', $match[1]) . str_replace(['\\', '_'], '/', $match[2]) . '.php';
18-
if (in_array($classname, ['PHPDraft', 'Mitchelf', 'QL']) !== FALSE) {
19+
if (in_array($classname, ['PHPDraft', 'Mitchelf', 'QL']) !== false) {
1920
include_once $classname;
2021
}
2122
}

src/PHPDraft/In/ApibFileParser.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file contains the ApibFileParser.
45
*
@@ -33,7 +34,7 @@ class ApibFileParser
3334
/**
3435
* Filename to parse.
3536
*
36-
* @var
37+
* @var string
3738
*/
3839
private $filename;
3940

@@ -75,15 +76,18 @@ public function parse(): self
7576
*
7677
* @return string The full API blueprint file.
7778
*/
78-
private function get_apib(string $filename, ?string $rel_path = NULL): string
79+
private function get_apib(string $filename, ?string $rel_path = null): string
7980
{
8081
$path = $this->file_path($filename, $rel_path);
8182
$file = file_get_contents($path);
8283
$matches = [];
8384
preg_match_all('<!-- include\(([\S\s]*?)(\.[a-z]*?)\) -->', $file, $matches);
8485
for ($i = 0; $i < count($matches[1]); $i++) {
85-
$file = str_replace('<!-- include(' . $matches[1][$i] . $matches[2][$i] . ') -->',
86-
$this->get_apib($matches[1][$i] . $matches[2][$i], dirname($path)), $file);
86+
$file = str_replace(
87+
'<!-- include(' . $matches[1][$i] . $matches[2][$i] . ') -->',
88+
$this->get_apib($matches[1][$i] . $matches[2][$i], dirname($path)),
89+
$file
90+
);
8791
}
8892

8993
preg_match_all('<!-- schema\(([a-z0-9_.\/\:]*?)\) -->', $file, $matches);
@@ -104,15 +108,15 @@ private function get_apib(string $filename, ?string $rel_path = NULL): string
104108
*
105109
* @return string
106110
*/
107-
private function file_path(string $filename, ?string $rel_path = NULL): string
111+
private function file_path(string $filename, ?string $rel_path = null): string
108112
{
109113
// Absolute path
110114
if (file_exists($filename)) {
111115
return $filename;
112116
}
113117

114118
// Path relative to the top file
115-
if ($rel_path !== NULL && file_exists($rel_path . $filename)) {
119+
if ($rel_path !== null && file_exists($rel_path . $filename)) {
116120
return $rel_path . $filename;
117121
}
118122

@@ -122,7 +126,7 @@ private function file_path(string $filename, ?string $rel_path = NULL): string
122126
}
123127

124128
$included_path = stream_resolve_include_path($filename);
125-
if ($included_path !== FALSE) {
129+
if ($included_path !== false) {
126130
return $included_path;
127131
}
128132

src/PHPDraft/In/Tests/ApibFileParserTest.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file contains the ApibFileParserTest.php
45
*
@@ -82,19 +83,23 @@ public function testParseBasic(): void
8283
$loc_property->setAccessible(true);
8384
$loc_property->setValue($this->class, TEST_STATICS . '/drafter/apib/');
8485

85-
$this->mock_function('curl_exec', function() { return 'hello';});
86+
$this->mock_function('curl_exec', function () {
87+
return 'hello';
88+
});
8689
$this->class->parse();
8790
$this->unmock_function('curl_exec');
8891

8992
$full_property = $this->reflection->getProperty('full_apib');
9093
$full_property->setAccessible(true);
9194

92-
$text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\nEXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n";
93-
$text .="# Tesla Model S JSON API\nThis is unofficial documentation of the Tesla Model S JSON API used by the iOS and Android apps. It features functionality to monitor and control the Model S remotely.\n\nTEST";
94-
$text .="\n\n# Hello\nThis is a test.\nhello";
95+
$text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\n";
96+
$text .="EXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n";
97+
$text .= "# Tesla Model S JSON API\nThis is unofficial documentation of the";
98+
$text .=" Tesla Model S JSON API used by the iOS and Android apps. It features";
99+
$text .=" functionality to monitor and control the Model S remotely.\n\nTEST";
100+
$text .= "\n\n# Hello\nThis is a test.\nhello";
95101

96102
$this->assertSame($text, $full_property->getValue($this->class));
97103
$this->assertSame($text, $this->class->__toString());
98104
}
99-
100105
}

src/PHPDraft/Model/Category.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file contains the Category.php.
45
*
@@ -29,7 +30,7 @@ class Category extends HierarchyElement
2930
*
3031
* @var ?string
3132
*/
32-
public $type = NULL;
33+
public $type = null;
3334

3435
/**
3536
* Fill class values based on JSON object.
@@ -42,7 +43,7 @@ public function parse(stdClass $object)
4243
{
4344
parent::parse($object);
4445

45-
$this->type = $object->meta->classes->content ?? NULL;
46+
$this->type = $object->meta->classes->content ?? null;
4647

4748
foreach ($object->content as $item) {
4849
switch ($item->element) {
@@ -67,7 +68,6 @@ public function parse(stdClass $object)
6768
break;
6869
default:
6970
continue 2;
70-
break;
7171
}
7272
}
7373

src/PHPDraft/Model/Comparable.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: smillernl

src/PHPDraft/Model/Elements/ArrayStructureElement.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file contains the ArrayStructureElement.php.
45
*
@@ -17,12 +18,12 @@ class ArrayStructureElement extends BasicStructureElement
1718
/**
1819
* Parse an array object.
1920
*
20-
* @param \stdClass $object APIb Item to parse
21-
* @param array $dependencies List of dependencies build
21+
* @param object $object APIB Item to parse
22+
* @param array $dependencies List of dependencies build
2223
*
2324
* @return self Self reference
2425
*/
25-
public function parse($object, array &$dependencies): StructureElement
26+
public function parse(object $object, array &$dependencies): StructureElement
2627
{
2728
$this->element = (isset($object->element)) ? $object->element : 'array';
2829

@@ -61,8 +62,11 @@ public function __toString(): string
6162
}
6263

6364
foreach ($this->value as $item) {
64-
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
65-
strtolower($item)) . '">' . $item . '</a>';
65+
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(
66+
' ',
67+
'-',
68+
strtolower($item)
69+
) . '">' . $item . '</a>';
6670

6771
$return .= '<li class="list-group-item mdl-list__item">' . $type . '</li>';
6872
}

0 commit comments

Comments
 (0)