Skip to content

Commit

Permalink
make github work
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 6, 2024
1 parent f545605 commit 9dad1fa
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 27 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/code-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Code Analysis

on:
pull_request: null
push:
branches:
- main

jobs:
code_analysis:
strategy:
fail-fast: false
matrix:
actions:
-
name: 'PHPStan'
run: composer phpstan

-
name: 'Check Active Classes'
run: vendor/bin/class-leak check src tests --ansi --skip-path=Fixture --skip-path=Source

-
name: 'Unit tests'
run: vendor/bin/phpunit

-
name: "Finalize classes"
run: vendor/bin/swiss-knife finalize-classes bin src tests --dry-run --ansi

-
name: 'Composer dependency Analyser'
run: vendor/bin/composer-dependency-analyser

-
name: 'Validate Composer'
run: composer validate

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none

- uses: "ramsey/composer-install@v2"

- run: ${{ matrix.actions.run }}
3 changes: 1 addition & 2 deletions bin/handyman.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

// 1. install require-dev
// WIP...
declare(strict_types=1);

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
Expand Down
11 changes: 11 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;

$configuration = new Configuration();

// available transitionally via phpstan, to keep compatible version
return $configuration->ignoreErrorsOnPackage('nikic/php-parser', [ErrorType::DEV_DEPENDENCY_IN_PROD]);
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "tomasvotruba/handyman",
"type": "phpstan-extension",
"license": "MIT",
"description": "Automate repeated steps when coming to a PHP project or create new package",
"require": {
"php": "^8.2",
Expand All @@ -9,7 +10,8 @@
"symfony/process": "^7.1",
"symfony/finder": "^7.1",
"nette/utils": "^4.0",
"phpstan/phpstan": "^1.12"
"phpstan/phpstan": "^1.12",
"webmozart/assert": "^1.11"
},
"require-dev": {
"rector/rector": "^1.2",
Expand All @@ -20,7 +22,7 @@
"symplify/easy-coding-standard": "^12.3",
"shipmonk/composer-dependency-analyser": "^1.7",
"tomasvotruba/type-coverage": "^1.0",
"tomasvotruba/class-leak": "^1.0",
"tomasvotruba/class-leak": "^1.1",
"tomasvotruba/unused-public": "^1.0",
"tracy/tracy": "^2.10",
"nikic/php-parser": "^4.19"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/PHPStan/Rule/PublicStaticDataProviderRule/Fixture</exclude>
</testsuite>
</testsuites>
</phpunit>
23 changes: 14 additions & 9 deletions src/Command/GithubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace TomasVotruba\Handyman\Command;

use Nette\Utils\FileSystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TomasVotruba\Handyman\FileSystem\TemplateFileSystem;
use TomasVotruba\Handyman\ValueObject\ComposerJson;

final class GithubCommand extends Command
{
Expand All @@ -27,17 +27,22 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$codeAnalysisFilePath = getcwd() . '/.github/workflows/code-analysis.yaml';
if (! file_exists($codeAnalysisFilePath)) {
$fileContents = TemplateFileSystem::renderFilePathWithVariables(
__DIR__ . '/../../templates/github-workflows/code-analysis.yaml',
[]
$projectFilePath = getcwd() . '/.github/workflows/code-analysis.yaml';

if (! file_exists($projectFilePath)) {
$projectComposerJson = new ComposerJson(getcwd() . '/composer.json');

TemplateFileSystem::renderFilePathWithVariables(
__DIR__ . '/../../templates/.github/workflows/code_analysis.yaml',
[
'__PHP_VERSION__' => $projectComposerJson->getPhpVersionString(),
],
$projectFilePath
);

FileSystem::write($codeAnalysisFilePath, $fileContents);
$this->symfonyStyle->success('Created .github/workflows/code-analysis.yaml');
$this->symfonyStyle->success('Created .github/workflows/code_analysis.yaml');
} else {
$this->symfonyStyle->success('Config .github/workflows/code-analysis.yaml already exists');
$this->symfonyStyle->success('Config .github/workflows/code_analysis.yaml already exists');
}

return self::SUCCESS;
Expand Down
21 changes: 17 additions & 4 deletions src/FileSystem/TemplateFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@
namespace TomasVotruba\Handyman\FileSystem;

use Nette\Utils\FileSystem;
use Webmozart\Assert\Assert;

final class TemplateFileSystem
{
/**
* @param array<string, string|float|int|null> $variables
*/
public static function renderFilePathWithVariables(string $sourceFilePath, array $variables): string
{
$fileContents = FileSystem::read($sourceFilePath);
return str_replace(array_keys($variables), array_values($variables), $fileContents);
public static function renderFilePathWithVariables(
string $sourceFilePath,
array $variables,
?string $targetFilePath = null
): string {
Assert::fileExists($sourceFilePath);

$templateContents = FileSystem::read($sourceFilePath);
$fileContents = str_replace(array_keys($variables), array_values($variables), $templateContents);

// save file contents directly
if (is_string($targetFilePath)) {
FileSystem::write($targetFilePath, $fileContents);
}

return $fileContents;
}
}
40 changes: 40 additions & 0 deletions src/ValueObject/ComposerJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\Handyman\ValueObject;

use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use Webmozart\Assert\Assert;

final class ComposerJson
{
private string $composerJsonFilePath;

/**
* @var array<string, mixed>
*/
private array $json = [];

public function __construct(string $composerJsonFilePath)
{
Assert::fileExists($composerJsonFilePath);
$this->composerJsonFilePath = $composerJsonFilePath;

$this->json = Json::decode(FileSystem::read($this->composerJsonFilePath), true);
}

public function getPhpVersionString(): string
{
$requirePhp = $this->json['require']['php'] ?? null;
Assert::string($requirePhp);

$match = Strings::match($requirePhp, '#(?<version>\d\.\d)#');
Assert::isArray($match);
Assert::keyExists($match, 'version');

return $match['version'];
}
}
11 changes: 11 additions & 0 deletions stubs/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Doctrine\ORM;

if (class_exists(EntityManager::class)) {
return;
}

class EntityManager
{
}
11 changes: 11 additions & 0 deletions stubs/Doctrine/Persistence/ObjectManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Doctrine\Persistence;

if (class_exists(ObjectManager::class)) {
return;
}

class ObjectManager
{
}
8 changes: 8 additions & 0 deletions stubs/Symfony/Contracts/Service/Attribute/Required.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Contracts\Service\Attribute;

class Required
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ jobs:

-
name: "Finalize classes"
run: vendor/bin/swiss-knife finalize-classes src tests --dry-run
run: vendor/bin/swiss-knife finalize-classes src tests --dry-run --ansi

-
name: 'Composer dependency Analyser'
run: vendor/bin/composer-dependency-analyser

-
name: 'Validate Composer'
run: composer validate

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/MockingEntity.php', [[NoEntityMockingRule::ERROR_MESSAGE, 12]]];

yield [__DIR__ . '/Fixture/MockingDocument.php', [[NoEntityMockingRule::ERROR_MESSAGE, 13]]];
yield [__DIR__ . '/Fixture/MockingDocument.php', [[NoEntityMockingRule::ERROR_MESSAGE, 12]]];

yield [__DIR__ . '/Fixture/SkipMockingOtherObject.php', []];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace TomasVotruba\Handyman\Tests\PHPStan\Rule\NoEntityMockingRule\Source;

class SimpleObject
final class SimpleObject
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace PHPStan\Rule\NoGetRepositoryOutsideServiceRule\Fixture;
namespace TomasVotruba\Handyman\Tests\PHPStan\Rule\NoGetRepositoryOutsideServiceRule\Fixture;

use Doctrine\ORM\EntityManager;
use PHPStan\Rule\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;
use TomasVotruba\Handyman\Tests\PHPStan\Rule\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;

final readonly class SkipInRepository
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/NonRepositoryUsingEntityManager.php', [[
NoGetRepositoryOutsideServiceRule::ERROR_MESSAGE,
17,
18,
]]];

yield [__DIR__ . '/Fixture/SkipInRepository.php', []];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace PHPStan\Rule\NoGetRepositoryOutsideServiceRule\Source;
namespace TomasVotruba\Handyman\Tests\PHPStan\Rule\NoGetRepositoryOutsideServiceRule\Source;

class SomeRandomEntity
final class SomeRandomEntity
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace TomasVotruba\Handyman\Tests\PHPStan\Rule\NoMockOnlyTestRule\Source;

class FirstClass
final class FirstClass
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace TomasVotruba\Handyman\Tests\PHPStan\Rule\NoMockOnlyTestRule\Source;

class SecondClass
final class SecondClass
{
}

0 comments on commit 9dad1fa

Please sign in to comment.