Skip to content

Commit

Permalink
Add github action to check php CS
Browse files Browse the repository at this point in the history
Reviewed-by: Alexandre Eher <alexandre.eher@werkspot.nl>
Reviewed-by: Viktor Szépe <viktor@szepe.net>
Co-Authored-by: Jefersson Nathan <malukenho.dev@gmail.com>
Signed-off-by: Thomas P <thomas.perez@travaux.com>
  • Loading branch information
ScullWM and malukenho committed Nov 13, 2020
1 parent 5eb3e96 commit 19226e2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: PHP tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer update --prefer-dist --no-progress

- name: Run code style checker
run: ./vendor/bin/phpcs
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"name": "codelicia/xulieta-json",
"autoload": {"psr-4": {"Codelicia\\XulietaJson\\": "src/"}},
"description": "Xulieta Plugin for JSON validation",
"license": "MIT",
"autoload": {
"psr-4": {
"Codelicia\\XulietaJson\\": "src/"
}
},
"require-dev": {
"doctrine/coding-standard": "^8.2.0",
"codelicia/xulieta": "^0.1.3"
}
}
18 changes: 18 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<ruleset
name="Xulieta"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<arg value="p"/>

<file>src</file>

<rule ref="Doctrine" />
</ruleset>
63 changes: 34 additions & 29 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@
namespace Codelicia\XulietaJson;

// @TODO rename
use Codelicia\Xulieta\Plugin\Plugin as ICodeliciaPlugin;
use Codelicia\Xulieta\Output\OutputFormatter;
use Codelicia\Xulieta\External\Markinho;
use Codelicia\Xulieta\Output\OutputFormatter;
use Codelicia\Xulieta\Plugin\Plugin as ICodeliciaPlugin;
use Codelicia\Xulieta\ValueObject\Violation;
use Symfony\Component\Finder\SplFileInfo;
use Throwable;

use function json_decode;

use const JSON_THROW_ON_ERROR;

final class Plugin implements ICodeliciaPlugin
{
/** @psalm-return list<non-empty-string> */
public function supportedExtensions(): array
{
return ['md', 'rst'];
}

public function canHandle(SplFileInfo $file): bool
{
return true;
}

public function __invoke(SplFileInfo $file, OutputFormatter $output): bool
{
foreach (Markinho::extractCodeBlocks($file->getPathname(), $file->getContents()) as $codeBlock) {
if ($codeBlock->language() !== 'json') {
continue;
}

try {
json_decode($codeBlock->code(), true, 512, JSON_THROW_ON_ERROR);
} catch (\Exception $ex) {
$output->addViolation(new Violation($codeBlock, $ex->getMessage(), 0));

return false;
}
}
}
/** @psalm-return list<non-empty-string> */
public function supportedExtensions(): array
{
return ['md', 'rst'];
}

public function canHandle(SplFileInfo $file): bool
{
return true;
}

public function __invoke(SplFileInfo $file, OutputFormatter $output): bool
{
foreach (Markinho::extractCodeBlocks($file->getPathname(), $file->getContents()) as $codeBlock) {
if ($codeBlock->language() !== 'json') {
continue;
}

try {
json_decode($codeBlock->code(), true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $ex) {
$output->addViolation(new Violation($codeBlock, $ex->getMessage(), 0));

return false;
}
}
}
}

0 comments on commit 19226e2

Please sign in to comment.