Skip to content

Commit

Permalink
Lint neon files in CI pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
shochdoerfer committed Dec 23, 2021
1 parent d430505 commit c2247cb
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ jobs:
- name: Codesniffer
run: composer cs-check

- name: Neon linting
run: |
./bin/ci_neon_lint
./bin/ci_markdown_neon_lint
- name: Static code analysis
run: composer analyze

Expand Down
57 changes: 57 additions & 0 deletions bin/ci_markdown_neon_lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env php
<?php

/*
* This file is part of the phpstan-magento package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

// inspired by https://github.com/mathiasverraes/uptodocs this CLI script will lint
// the Neon snippets in the Markdown files like README.md

$path = realpath(__DIR__ . '/../');
$it = new RecursiveDirectoryIterator($path);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
$it = new RegexIterator($it, '~\.md$~');

$environment = new \League\CommonMark\Environment();
$environment->addExtension(new \League\CommonMark\Extension\CommonMarkCoreExtension());
$parser = new \League\CommonMark\DocParser($environment);

$success = true;
foreach ($it as $file) {
/** @var SplFileInfo $file */
if (strpos($file->getRealPath(), '/vendor/') !== false) {
continue;
}

$nodeCount = 0;
$content = file_get_contents($file->getRealPath());
$document = $parser->parse($content);
$walker = $document->walker();
while ($event = $walker->next()) {
if ($event->getNode() instanceof \League\CommonMark\Block\Element\FencedCode
&& $event->isEntering()
&& $event->getNode()->getInfo() === 'neon') {

try {
$nodeCount++;
$node = $event->getNode();
Nette\Neon\Neon::decode($codeBlock = $node->getStringContent());
} catch (Nette\Neon\Exception $e) {
$success = false;
$relPath = str_replace($path . DIRECTORY_SEPARATOR, '', $file->getRealPath());
echo "Failed parsing node ".$nodeCount." of file ".$relPath."\n";
}
}
}
}

exit($success ? 0 : 1);
40 changes: 40 additions & 0 deletions bin/ci_neon_lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env php
<?php

/*
* This file is part of the phpstan-magento package.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

// this CLI script will lint all the .neon files in the repository

$path = realpath(__DIR__ . '/../');
$it = new RecursiveDirectoryIterator($path);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
$it = new RegexIterator($it, '~\.neon$~');

$success = true;
foreach ($it as $file) {
/** @var SplFileInfo $file */
if (strpos($file->getRealPath(), '/vendor/') !== false) {
continue;
}

try {
$content = file_get_contents($file->getRealPath());
Nette\Neon\Neon::decode($content);
} catch (Nette\Neon\Exception $e) {
$success = false;
$relPath = str_replace($path . DIRECTORY_SEPARATOR, '', $file->getRealPath());
echo "Failed parsing ".$relPath."\n";
}
}

exit($success ? 0 : 1);
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
"require-dev": {
"captainhook/captainhook": "^5.10.4",
"captainhook/plugin-composer": "^5.3.2",
"league/commonmark": "^1.4",
"madewithlove/license-checker": "^0.10.0",
"magento/framework": ">=102.0.0",
"mikey179/vfsstream": "^1.6.10",
"nette/neon": "^3.3.1",
"nikic/php-parser": "^4.13.1",
"phpstan/extension-installer": "^1.1.0",
"phpstan/phpstan-phpunit": "^1.0.0",
Expand Down
171 changes: 170 additions & 1 deletion composer.lock

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

4 changes: 2 additions & 2 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Since Magento framework version 100.1.0 entities must not be responsible for the
be used to persist entities.

To disable this rule add the following code to your `phpstan.neon` configuration file:
```
```neon
parameters:
magento:
checkServiceContracts: false
Expand All @@ -54,7 +54,7 @@ Since Magento framework version 101.0.0 Collections should be used directly via
`\Magento\Framework\Model\AbstractModel::getCollection()` directly.

To disable this rule add the following code to your `phpstan.neon` configuration file:
```
```neon
parameters:
magento:
checkCollectionViaFactory: false
Expand Down

0 comments on commit c2247cb

Please sign in to comment.