Skip to content

Commit

Permalink
✨ use grumphp-config for linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Jul 31, 2023
1 parent 3556be6 commit e89a684
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
var/
71 changes: 39 additions & 32 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
{
"name": "pluswerk/grumphp-xliff-task",
"description": "GrumPHP task to lint xlf/xliff Files",
"type": "library",
"license": "LGPL-3.0-or-later",
"authors": [
{
"name": "Matthias Vogel",
"email": "m.vogel@andersundsehr.com",
"homepage": "https://www.andersundsehr.com"
}
],
"support": {
"issues": "https://github.com/pluswerk/grumphp-xliff-task/issues"
},
"autoload": {
"psr-4": {
"PLUS\\GrumPHPXliffTask\\": "src/"
}
},
"require": {
"ext-dom": "*",
"php": "~8.1.0 || ~8.2.0",
"phpro/grumphp": "^2",
"opis/closure": "^3.6.2"
},
"require-dev": {
"squizlabs/php_codesniffer": ">=3.5.0 <4.0.0"
},
"config": {
"allow-plugins": {
"phpro/grumphp": true
}
"name": "pluswerk/grumphp-xliff-task",
"description": "GrumPHP task to lint xlf/xliff Files",
"license": "LGPL-3.0-or-later",
"type": "library",
"authors": [
{
"name": "Matthias Vogel",
"email": "m.vogel@andersundsehr.com",
"homepage": "https://www.andersundsehr.com"
}
],
"support": {
"issues": "https://github.com/pluswerk/grumphp-xliff-task/issues"
},
"require": {
"php": "~8.1.0 || ~8.2.0",
"ext-dom": "*",
"phpro/grumphp": "^2"
},
"require-dev": {
"pluswerk/grumphp-config": "^6.8.0"
},
"autoload": {
"psr-4": {
"PLUS\\GrumPHPXliffTask\\": "src/"
}
},
"config": {
"allow-plugins": {
"phpro/grumphp": true,
"phpstan/extension-installer": true,
"ergebnis/composer-normalize": true,
"pluswerk/grumphp-config": true
}
},
"extra": {
"branch-alias": {
"dev-main": "6.0.x-dev"
}
}
}
41 changes: 16 additions & 25 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
grumphp:
tasks:
composer:
with_dependencies: false
strict: false
git_blacklist:
keywords:
- "die("
- "var_dump("
- "exit;"
triggered_by: [php]
git_commit_message:
max_subject_width: 120
max_body_width: 120
enforce_capitalized_subject: false
jsonlint:
detect_key_conflicts: true
phpcs:
standard: "PSR12"
warning_severity: 0
tab_width: 4
yamllint: ~
xlifflint: ~
extensions:
- PLUS\GrumPHPXliffTask\ExtensionLoader
imports:
- { resource: vendor/pluswerk/grumphp-config/grumphp.yml }
parameters:
convention.process_timeout: 240
convention.security_checker_blocking: true
convention.jsonlint_ignore_pattern: { }
convention.xmllint_ignore_pattern: { }
convention.yamllint_ignore_pattern: { }
convention.phpcslint_ignore_pattern: { }
convention.phpcslint_exclude: { }
convention.xlifflint_ignore_pattern: { }
convention.rector_ignore_pattern: { }
convention.rector_enabled: true
convention.rector_config: rector.php
convention.rector_clear-cache: false
convention.phpstan_level: null
2 changes: 2 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors: []
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan-baseline.neon
- vendor/andersundsehr/phpstan-git-files/extension.php

parameters:
level: 8
reportUnmatchedIgnoredErrors: false
42 changes: 42 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use PLUS\GrumPHPConfig\RectorSettings;
use Rector\Config\RectorConfig;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->parallel();
$rectorConfig->importNames();
$rectorConfig->importShortClasses();
$rectorConfig->cacheClass(FileCacheStorage::class);
$rectorConfig->cacheDirectory('./var/cache/rector');

$rectorConfig->paths(
array_filter(explode("\n", (string)shell_exec("git ls-files | xargs ls -d 2>/dev/null | grep -E '\.(php|html|typoscript)$'")))
);

// define sets of rules
$rectorConfig->sets(
[
...RectorSettings::sets(true),
...RectorSettings::setsTypo3(false),
]
);

// remove some rules
// ignore some files
$rectorConfig->skip(
[
...RectorSettings::skip(),
...RectorSettings::skipTypo3(),

/**
* rector should not touch these files
*/
//__DIR__ . '/src/Example',
//__DIR__ . '/src/Example.php',
]
);
};
5 changes: 3 additions & 2 deletions src/ExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace PLUS\GrumPHPXliffTask;

use Iterator;
use GrumPHP\Extension\ExtensionInterface;

class ExtensionLoader implements ExtensionInterface
final class ExtensionLoader implements ExtensionInterface
{
public function imports(): iterable
public function imports(): Iterator
{
yield __DIR__ . '/../Services.yaml';
}
Expand Down
15 changes: 12 additions & 3 deletions src/XliffLint.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace PLUS\GrumPHPXliffTask;

use GrumPHP\Exception\RuntimeException;
use GrumPHP\Linter\LinterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\AbstractLinterTask;
Expand All @@ -12,7 +15,10 @@
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

class XliffLint extends AbstractLinterTask
/**
* @extends AbstractLinterTask<XliffLinter>
*/
final class XliffLint extends AbstractLinterTask
{
/** @var XliffLinter */
protected $linter;
Expand Down Expand Up @@ -49,18 +55,21 @@ public function run(ContextInterface $context): TaskResultInterface
if (0 === count($files)) {
return TaskResult::createSkipped($this, $context);
}

$this->linter->setLoadFromNet($options['load_from_net']);
$this->linter->setXInclude($options['x_include']);
$this->linter->setDtdValidation($options['dtd_validation']);
$this->linter->setSchemeValidation($options['scheme_validation']);
try {
$lintErrors = $this->lint($files);
} catch (RuntimeException $e) {
return TaskResult::createFailed($this, $context, $e->getMessage());
} catch (RuntimeException $runtimeException) {
return TaskResult::createFailed($this, $context, $runtimeException->getMessage());
}

if ($lintErrors->count()) {
return TaskResult::createFailed($this, $context, (string)$lintErrors);
}

return TaskResult::createPassed($this, $context);
}
}
17 changes: 13 additions & 4 deletions src/XliffLinter.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?php

declare(strict_types=1);

namespace PLUS\GrumPHPXliffTask;

use DOMDocument;
use DOMElement;
use GrumPHP\Collection\LintErrorsCollection;
use GrumPHP\Linter\LintError;
use GrumPHP\Linter\Xml\XmlLinter;
use SplFileInfo;

class XliffLinter extends XmlLinter
final class XliffLinter extends XmlLinter
{
public function lint(SplFileInfo $file): LintErrorsCollection
{
$lintErrors = parent::lint($file);
if ($lintErrors->count()) {
return $lintErrors;
}

$document = new DOMDocument();
if (!$document->load($file->getPathname())) {
$lintErrors->add(
Expand All @@ -30,9 +34,10 @@ public function lint(SplFileInfo $file): LintErrorsCollection
}

$rootElement = $document->documentElement;
/** @var DOMElement[] $fileTags */
assert($rootElement instanceof DOMElement);
$fileTags = $rootElement->getElementsByTagName('file');
foreach ($fileTags as $fileTag) {
assert($fileTag instanceof DOMElement);
if ($fileTag->attributes->getNamedItem('target')) {
$lintErrors->add(
new LintError(
Expand All @@ -44,10 +49,11 @@ public function lint(SplFileInfo $file): LintErrorsCollection
);
continue;
}

if ($fileTag->attributes->getNamedItem('target-language')) {
/** @var DOMElement[] $transUnitTags */
$transUnitTags = $fileTag->getElementsByTagName('trans-unit');
foreach ($transUnitTags as $transUnitTag) {
assert($transUnitTag instanceof DOMElement);
if (!$transUnitTag->attributes->getNamedItem('id')) {
$lintErrors->add(
new LintError(
Expand All @@ -58,6 +64,7 @@ public function lint(SplFileInfo $file): LintErrorsCollection
)
);
}

if (!$transUnitTag->getElementsByTagName('target')->length) {
$lintErrors->add(
new LintError(
Expand All @@ -70,9 +77,9 @@ public function lint(SplFileInfo $file): LintErrorsCollection
}
}
} else {
/** @var DOMElement[] $transUnitTags */
$transUnitTags = $fileTag->getElementsByTagName('trans-unit');
foreach ($transUnitTags as $transUnitTag) {
assert($transUnitTag instanceof DOMElement);
if (!$transUnitTag->attributes->getNamedItem('id')) {
$lintErrors->add(
new LintError(
Expand All @@ -83,6 +90,7 @@ public function lint(SplFileInfo $file): LintErrorsCollection
)
);
}

if (!$transUnitTag->getElementsByTagName('source')->length) {
$lintErrors->add(
new LintError(
Expand All @@ -93,6 +101,7 @@ public function lint(SplFileInfo $file): LintErrorsCollection
)
);
}

if ($targetElement = $transUnitTag->getElementsByTagName('target')->item(0)) {
$lintErrors->add(
new LintError(
Expand Down

0 comments on commit e89a684

Please sign in to comment.