Skip to content

Commit

Permalink
Fix based on static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Feb 4, 2020
1 parent 1ee2cce commit 4368e3a
Show file tree
Hide file tree
Showing 59 changed files with 712 additions and 444 deletions.
19 changes: 7 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ on:
- master

jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: PHP Code Scanner Scan
uses: rtCamp/action-phpcs-code-review@master
continue-on-error: true
env:
GH_BOT_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
args: "PSR12"
test:
runs-on: ubuntu-latest
name: PHP ${{ matrix.php-versions }}
Expand All @@ -36,7 +25,7 @@ jobs:
php-version: ${{ matrix.php-versions }}
extensions: curl,json,mbstring,uopz
coverage: pcov
tools: pecl
tools: pecl,phpstan,phpunit,cs2pr,phpcs

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

- name: PHPStan
run: phpstan analyse --level=3 --error-format=checkstyle src/ | cs2pr

- name: PHPCS
run: phpcs --standard=PSR12 --ignore=\*/Tests/\*,\*Minifier.php --exclude=PSR1.Methods.CamelCapsMethodName,Generic.Files.LineLength src/ | cs2pr

- name: Upload coverage result
uses: actions/upload-artifact@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.idea/
/.phpintel/
/tests/.phpunit.result.cache

# IntelliJ
/build/coverage
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"lunr/halo": "dev-master",
"phpunit/phpunit": "~8.0",
"theseer/autoload": "~1.0",
"phing/phing": "~2.0"
"phing/phing": "~2.0",
"phpstan/phpstan-phpunit": "^0.12.6",
"phpstan/phpstan": "^0.12.8"
},
"scripts": {
"test": [
Expand Down
97 changes: 96 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use PHPDraft\In\ApibFileParser;
use PHPDraft\Out\Sorting;
use PHPDraft\Out\Version;
use PHPDraft\Parse\ExecutionException;
use PHPDraft\Parse\JsonToHTML;
use PHPDraft\Parse\LegacyHtmlGenerator;
use PHPDraft\Parse\ParserFactory;
use PHPDraft\Parse\ResourceException;

Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
bootstrap: tests/test.bootstrap.inc.php
ignoreErrors:
- '#Access to an undefined property object::\$[a-zA-Z0-9\\_]+#'
- '#Access to an undefined property PHPUnit\\Framework\\MockObject\\MockObject::\$[a-zA-Z0-9_]+#'
- '#Access to an undefined property PHPDraft\\Model\\HierarchyElement::\$[a-zA-Z0-9_]+#'
3 changes: 2 additions & 1 deletion src/PHPDraft/Core/Autoloader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file contains the Autoloader.
*
Expand All @@ -15,7 +16,7 @@ function ($classname) {
$classname = ltrim($classname, '\\');
preg_match('/^(.+)?([^\\\\]+)$/U', $classname, $match);
$classname = str_replace('\\', '/', $match[1]) . str_replace(['\\', '_'], '/', $match[2]) . '.php';
if (in_array($classname, ['PHPDraft', 'Mitchelf', 'QL']) !== FALSE) {
if (in_array($classname, ['PHPDraft', 'Mitchelf', 'QL']) !== false) {
include_once $classname;
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/PHPDraft/In/ApibFileParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file contains the ApibFileParser.
*
Expand Down Expand Up @@ -33,7 +34,7 @@ class ApibFileParser
/**
* Filename to parse.
*
* @var
* @var string
*/
private $filename;

Expand Down Expand Up @@ -75,15 +76,18 @@ public function parse(): self
*
* @return string The full API blueprint file.
*/
private function get_apib(string $filename, ?string $rel_path = NULL): string
private function get_apib(string $filename, ?string $rel_path = null): string
{
$path = $this->file_path($filename, $rel_path);
$file = file_get_contents($path);
$matches = [];
preg_match_all('<!-- include\(([\S\s]*?)(\.[a-z]*?)\) -->', $file, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$file = str_replace('<!-- include(' . $matches[1][$i] . $matches[2][$i] . ') -->',
$this->get_apib($matches[1][$i] . $matches[2][$i], dirname($path)), $file);
$file = str_replace(
'<!-- include(' . $matches[1][$i] . $matches[2][$i] . ') -->',
$this->get_apib($matches[1][$i] . $matches[2][$i], dirname($path)),
$file
);
}

preg_match_all('<!-- schema\(([a-z0-9_.\/\:]*?)\) -->', $file, $matches);
Expand All @@ -104,15 +108,15 @@ private function get_apib(string $filename, ?string $rel_path = NULL): string
*
* @return string
*/
private function file_path(string $filename, ?string $rel_path = NULL): string
private function file_path(string $filename, ?string $rel_path = null): string
{
// Absolute path
if (file_exists($filename)) {
return $filename;
}

// Path relative to the top file
if ($rel_path !== NULL && file_exists($rel_path . $filename)) {
if ($rel_path !== null && file_exists($rel_path . $filename)) {
return $rel_path . $filename;
}

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

$included_path = stream_resolve_include_path($filename);
if ($included_path !== FALSE) {
if ($included_path !== false) {
return $included_path;
}

Expand Down
15 changes: 10 additions & 5 deletions src/PHPDraft/In/Tests/ApibFileParserTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file contains the ApibFileParserTest.php
*
Expand Down Expand Up @@ -82,19 +83,23 @@ public function testParseBasic(): void
$loc_property->setAccessible(true);
$loc_property->setValue($this->class, TEST_STATICS . '/drafter/apib/');

$this->mock_function('curl_exec', function() { return 'hello';});
$this->mock_function('curl_exec', function () {
return 'hello';
});
$this->class->parse();
$this->unmock_function('curl_exec');

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

$text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\nEXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n";
$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";
$text .="\n\n# Hello\nThis is a test.\nhello";
$text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\n";
$text .="EXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n";
$text .= "# Tesla Model S JSON API\nThis is unofficial documentation of the";
$text .=" Tesla Model S JSON API used by the iOS and Android apps. It features";
$text .=" functionality to monitor and control the Model S remotely.\n\nTEST";
$text .= "\n\n# Hello\nThis is a test.\nhello";

$this->assertSame($text, $full_property->getValue($this->class));
$this->assertSame($text, $this->class->__toString());
}

}
6 changes: 3 additions & 3 deletions src/PHPDraft/Model/Category.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file contains the Category.php.
*
Expand Down Expand Up @@ -29,7 +30,7 @@ class Category extends HierarchyElement
*
* @var ?string
*/
public $type = NULL;
public $type = null;

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

$this->type = $object->meta->classes->content ?? NULL;
$this->type = $object->meta->classes->content ?? null;

foreach ($object->content as $item) {
switch ($item->element) {
Expand All @@ -67,7 +68,6 @@ public function parse(stdClass $object)
break;
default:
continue 2;
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/PHPDraft/Model/Comparable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Created by PhpStorm.
* User: smillernl
Expand Down
14 changes: 9 additions & 5 deletions src/PHPDraft/Model/Elements/ArrayStructureElement.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file contains the ArrayStructureElement.php.
*
Expand All @@ -17,12 +18,12 @@ class ArrayStructureElement extends BasicStructureElement
/**
* Parse an array object.
*
* @param \stdClass $object APIb Item to parse
* @param array $dependencies List of dependencies build
* @param object $object APIB Item to parse
* @param array $dependencies List of dependencies build
*
* @return self Self reference
*/
public function parse($object, array &$dependencies): StructureElement
public function parse(object $object, array &$dependencies): StructureElement
{
$this->element = (isset($object->element)) ? $object->element : 'array';

Expand Down Expand Up @@ -61,8 +62,11 @@ public function __toString(): string
}

foreach ($this->value as $item) {
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(' ', '-',
strtolower($item)) . '">' . $item . '</a>';
$type = (in_array($item, self::DEFAULTS)) ? $item : '<a href="#object-' . str_replace(
' ',
'-',
strtolower($item)
) . '">' . $item . '</a>';

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

0 comments on commit 4368e3a

Please sign in to comment.