Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CI pipeline #1

Merged
merged 9 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .build/ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
// add a single rule
->withRules([
NoUnusedImportsFixer::class,
ArraySyntaxFixer::class,
])
->withPreparedSets(psr12: true)
;
48 changes: 48 additions & 0 deletions .github/workflows/lint_test_pull_requests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Code style & tests"
on:
pull_request:
# Run this workflow for all PRs against main
branches:
- main

jobs:
php-check:
runs-on: ubuntu-latest
strategy:
matrix:
composer-command:
- name: PHP Code Style
command: 'style:php'
- name: Composer normalize
command: 'normalize --dry-run'
- name: PHPStan
command: analyze:php
- name: PHPUnit
command: test:php
directory: ['extension-installer', 'fractor', 'fractor-xml', 'typo3-fractor']
exclude:
- directory: extension-installer
composer-command: {name: 'PHPUnit', command: 'test:php'}

name: ${{ matrix.composer-command.name }}/${{ matrix.directory }}

defaults:
run:
working-directory: ${{ matrix.directory }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Install composer dependencies
uses: php-actions/composer@v6
with:
container_workdir: /app/${{ matrix.directory }}
memory_limit: 512M

- name: 'Run check "${{ matrix.composer-command.name }}"'
uses: php-actions/composer@v6
with:
container_workdir: /app/${{ matrix.directory }}
command: ${{ matrix.composer-command.command }}
memory_limit: 512M
8 changes: 7 additions & 1 deletion extension-installer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"require-dev": {
"composer/composer": "^2.7",
"ergebnis/composer-normalize": "^2.42"
"ergebnis/composer-normalize": "^2.42",
"phpstan/phpstan": "^1.10",
"symplify/easy-coding-standard": "^12.1"
},
"autoload": {
"psr-4": {
Expand All @@ -31,5 +33,9 @@
},
"extra": {
"class": "a9f\\FractorExtensionInstaller\\InstallerPlugin"
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs"
}
}
9 changes: 9 additions & 0 deletions extension-installer/ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

return (include __DIR__ . '/../.build/ecs.php')
->withPaths([
__DIR__ . '/src',
])
;
5 changes: 5 additions & 0 deletions extension-installer/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8

paths:
- src/
2 changes: 1 addition & 1 deletion extension-installer/src/InstallerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public static function getSubscribedEvents(): array
ScriptEvents::POST_UPDATE_CMD => 'processEvent',
];
}
}
}
5 changes: 2 additions & 3 deletions extension-installer/src/PackagesFileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function __construct(
private readonly InstalledRepositoryInterface $repository,
private readonly InstallationManager $installationManager,
private readonly string $fileToGenerate
)
{
) {
}

public function generate(): void
Expand All @@ -49,4 +48,4 @@ public function generate(): void
sprintf(self::FILE_TEMPLATE, $installedPackagesCode)
);
}
}
}
21 changes: 14 additions & 7 deletions fractor-xml/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5"
"phpunit/phpunit": "^10.5",
"symplify/easy-coding-standard": "^12.1"
},
"repositories": {
"fractor": {
"type": "path",
"url": "../*"
}
},
"autoload": {
"psr-4": {
Expand All @@ -32,16 +39,16 @@
"a9f\\FractorXml\\Tests\\": "tests/"
}
},
"repositories": {
"fractor": {
"type": "path",
"url": "../*"
}
},
"config": {
"allow-plugins": {
"a9f/fractor-extension-installer": true,
"ergebnis/composer-normalize": true
},
"sort-packages": true
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs",
"test:php": "phpunit tests/"
}
}
2 changes: 1 addition & 1 deletion fractor-xml/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
->autoconfigure();

$services->load('a9f\\FractorXml\\', __DIR__ . '/../src/');
};
};
4 changes: 2 additions & 2 deletions fractor-xml/config/fractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
use a9f\Fractor\Configuration\FractorConfig;
use a9f\FractorXml\DependencyInjection\XmlFractorCompilerPass;

return static function(FractorConfig $config) {
return static function (FractorConfig $config) {
$config->addCompilerPass(new XmlFractorCompilerPass());
};
};
11 changes: 11 additions & 0 deletions fractor-xml/ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return (include __DIR__ . '/../.build/ecs.php')
->withPaths([
__DIR__ . '/config',
__DIR__ . '/src',
__DIR__ . '/tests',
])
;
6 changes: 6 additions & 0 deletions fractor-xml/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 8

paths:
- src/
- tests/
2 changes: 1 addition & 1 deletion fractor-xml/src/AbstractXmlFractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ public function afterTraversal(\DOMNode $rootNode): void
{
// no-op for now
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public function process(ContainerBuilder $container): void
$definition = $container->findDefinition(XmlFileProcessor::class);
$definition->setArgument('$rules', $references);
}
}
}
9 changes: 8 additions & 1 deletion fractor-xml/src/DomDocumentIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function traverseDocument(\DOMDocument $document): void
$visitor->beforeTraversal($document);
}

$this->traverseNode($document->firstChild);
if ($document->firstChild instanceof \DOMNode) {
$this->traverseNode($document->firstChild);
}

foreach ($this->visitors as $visitor) {
$visitor->afterTraversal($document);
Expand All @@ -33,6 +35,11 @@ private function traverseNode(\DOMNode $node): void
foreach ($this->visitors as $visitor) {
$result = $visitor->enterNode($node);

if ($node->parentNode === null) {
// TODO convert into a custom ShouldNotHappenException
throw new \RuntimeException('Node has no parent node');
}

if ($result === DomDocumentIterator::REMOVE_NODE) {
$node->parentNode->removeChild($node);
$nodeRemoved = true;
Expand Down
2 changes: 1 addition & 1 deletion fractor-xml/src/DomNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public function enterNode(\DOMNode $node): \DOMNode|int;
public function leaveNode(\DOMNode $node): void;

public function afterTraversal(\DOMNode $rootNode): void;
}
}
2 changes: 1 addition & 1 deletion fractor-xml/src/XmlFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ public function handle(\SplFileInfo $file): void
// TODO only update file if changed
file_put_contents($file->getPathname(), $doc->saveXML());
}
}
}
7 changes: 5 additions & 2 deletions fractor-xml/src/XmlFractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace a9f\FractorXml;

interface XmlFractor
interface XmlFractor extends DomNodeVisitor
{
public function canHandle(\DOMNode $node): bool;

/**
* @return \DOMNode|DomDocumentIterator::*|null
*/
public function refactor(\DOMNode $node): \DOMNode|int|null;
}
}
23 changes: 14 additions & 9 deletions fractor-xml/tests/DomDocumentIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function traversalEntersAndLeavesGrandChildrenBeforeLeavingChildren(): vo
#[Test]
public function nodeIsRemovedFromDomIfVisitorReturnsRemoveNode(): void
{
$nodeRemovingVisitor = new class extends CollectingDomNodeVisitor {
$nodeRemovingVisitor = new class() extends CollectingDomNodeVisitor {
public function enterNode(\DOMNode $node): \DOMNode|int
{
parent::enterNode($node);
Expand All @@ -150,7 +150,6 @@ public function enterNode(\DOMNode $node): \DOMNode|int
}
return $node;
}

};
$document = new \DOMDocument();
$document->loadXML(<<<XML
Expand All @@ -169,22 +168,25 @@ public function enterNode(\DOMNode $node): \DOMNode|int
'afterTraversal:#document',
], $nodeRemovingVisitor->calls);

self::assertXmlStringEqualsXmlString('<Root></Root>', $document->saveXML());
self::assertXmlStringEqualsXmlString('<Root></Root>', $document->saveXML() ?: '');
}

#[Test]
public function nodeIsReplacedIfVisitorReturnsNewDomNode(): void
{
$nodeRemovingVisitor = new class extends CollectingDomNodeVisitor {
$nodeRemovingVisitor = new class() extends CollectingDomNodeVisitor {
public function enterNode(\DOMNode $node): \DOMNode|int
{
parent::enterNode($node);
if ($node->nodeName === 'Child') {
if ($node->ownerDocument === null) {
throw new \RuntimeException('Node does not have an ownerDocument, cannot create element');
}

return $node->ownerDocument->createElement('NewChild');
}
return $node;
}

};
$document = new \DOMDocument();
$document->loadXML(<<<XML
Expand All @@ -203,14 +205,17 @@ public function enterNode(\DOMNode $node): \DOMNode|int
'afterTraversal:#document',
], $nodeRemovingVisitor->calls);

self::assertXmlStringEqualsXmlString('<Root><NewChild></NewChild></Root>', $document->saveXML());
self::assertXmlStringEqualsXmlString('<Root><NewChild></NewChild></Root>', $document->saveXML() ?: '');
}

private function getCollectingDomNodeVisitor(): DomNodeVisitor
private function getCollectingDomNodeVisitor(): CollectingDomNodeVisitor
{
return new CollectingDomNodeVisitor();
}

/**
* @param list<string> $recorder
*/
private function getCallRecordingDomNodeVisitor(string $visitorName, array &$recorder): DomNodeVisitor
{
return new class($visitorName, $recorder) implements DomNodeVisitor {
Expand All @@ -219,7 +224,7 @@ private function getCallRecordingDomNodeVisitor(string $visitorName, array &$rec
*/
public function __construct(
private readonly string $visitorName,
private array &$calls
public array &$calls // only public to please PHPStan
) {
}

Expand All @@ -245,4 +250,4 @@ public function afterTraversal(\DOMNode $rootNode): void
}
};
}
}
}
5 changes: 3 additions & 2 deletions fractor-xml/tests/Fixtures/CollectingDomNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use a9f\FractorXml\DomNodeVisitor;

class CollectingDomNodeVisitor implements DomNodeVisitor {
class CollectingDomNodeVisitor implements DomNodeVisitor
{
/** @var list<non-empty-string> */
public array $calls = [];

Expand All @@ -28,4 +29,4 @@ public function afterTraversal(\DOMNode $rootNode): void
{
$this->calls[] = sprintf('afterTraversal:%s', $rootNode->nodeName);
}
}
}
10 changes: 8 additions & 2 deletions fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
"phpstan/phpstan": "1.10",
"phpunit/phpunit": "^10.5"
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5",
"symplify/easy-coding-standard": "^12.1"
},
"autoload": {
"psr-4": {
Expand All @@ -41,5 +42,10 @@
"ergebnis/composer-normalize": true
},
"sort-packages": true
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs",
"test:php": "phpunit tests/"
}
}
2 changes: 1 addition & 1 deletion fractor/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

$services->set(FractorConfig::class)
->lazy(true);
};
};
Loading