Skip to content

Commit 509a84e

Browse files
committed
feat: adds configuration (squash 0)
1 parent b7cf443 commit 509a84e

5 files changed

Lines changed: 75 additions & 8 deletions

File tree

src/Commands/LintCommand.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Stolt\ReadmeLint\Commands;
66

77
use Stolt\ReadmeLint\Configuration;
8+
use Stolt\ReadmeLint\Configuration\Resolver as ConfigurationResolver;
89
use Stolt\ReadmeLint\Linter;
910
use Stolt\ReadmeLint\LintIssue;
1011
use Stolt\ReadmeLint\Rules\BadgeRule;
@@ -39,7 +40,7 @@ protected function configure(): void
3940
'rules',
4041
null,
4142
InputOption::VALUE_OPTIONAL,
42-
"Comma-separated list of lint rules to apply (e.g. RequiredSectionsRule, MaxLineLengthRule)"
43+
'Comma-separated list of lint rules to apply (e.g. RequiredSectionsRule, MaxLineLengthRule)'
4344
)->addOption(
4445
'config',
4546
null,
@@ -68,18 +69,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6869

6970
$linter = (new Linter($path));
7071
$rulesResolver = new Resolver();
72+
$configurationResolver = new ConfigurationResolver();
7173

72-
$viaOptionSetConfigPath = (string) $input->getOption('config');
74+
$viaOptionSetConfigurationPath = (string) $input->getOption('config');
7375
$viaOptionSetRules = (string) $input->getOption('rules');
7476

75-
if ($viaOptionSetConfigPath !== '') {
76-
if (!\file_exists($viaOptionSetConfigPath)) {
77-
$output->writeln('Configuration file <error>not</error> found at <info>' . $viaOptionSetConfigPath . '</info>');
77+
$configurationPath = $configurationResolver->resolveConfigurationPath($viaOptionSetConfigurationPath);
7878

79+
if ($configurationPath !== null) {
80+
if (!\file_exists($configurationPath)) {
81+
$output->writeln('Configuration file <error>not</error> found at <info>' . $configurationPath . '</info>');
7982
return Command::FAILURE;
8083
}
8184

82-
$config = require $viaOptionSetConfigPath;
85+
$config = require $configurationPath;
8386

8487
if ($config instanceof Configuration) {
8588
$linter->addRules($config->getRulesToApply());
@@ -90,10 +93,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9093
$linter->addRules($resolved);
9194
}
9295
}
93-
} elseif ($viaOptionSetRules !== '') {
96+
}
97+
98+
// Override config rules with --rules option if provided
99+
if ($viaOptionSetRules !== '') {
94100
$names = \array_values(\array_filter(\array_map('trim', \explode(',', $viaOptionSetRules))));
101+
// Clear previously added rules and use only the ones from --rules option
102+
$linter = (new Linter($path));
95103
$linter->addRules($rulesResolver->resolveRulesByNames($names));
96-
} else {
104+
} elseif ($configurationPath === null) {
105+
// No config found and no rules option provided, use defaults
97106
$linter->addRules([
98107
new RequiredSectionsRule(),
99108
new BadgeRule(),

src/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class Configuration
2121
/**
2222
* @param Linter $linter
2323
* @throws ReflectionException
24+
*
25+
* TODO: Remove linter dependency from configuration.
2426
*/
2527
public function __construct(Linter $linter)
2628
{

src/Configuration/Resolver.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\ReadmeLint\Configuration;
6+
7+
final class Resolver
8+
{
9+
/**
10+
* Resolves the configuration file path to use.
11+
*
12+
* Priority:
13+
* 1. Explicit --config option of the Lint command
14+
* 2. .readme-lint.php in the current directory
15+
* 3. null (no config)
16+
*/
17+
public function resolveConfigurationPath(?string $explicitConfigurationPath): ?string
18+
{
19+
if ($explicitConfigurationPath !== null && $explicitConfigurationPath !== '') {
20+
return $explicitConfigurationPath;
21+
}
22+
23+
$defaultConfigPath = \getcwd() . DIRECTORY_SEPARATOR . '.readme-lint.php';
24+
25+
// TODO: Validate that the file is valid PHP and returns a configuration object or array.
26+
27+
if (\file_exists($defaultConfigPath)) {
28+
return $defaultConfigPath;
29+
}
30+
31+
return null;
32+
}
33+
}

tests/Fixtures/.readme-lint.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
use Stolt\ReadmeLint\Configuration;
4+
use Stolt\ReadmeLint\Linter;
5+
6+
return (new Configuration(new Linter(\getcwd())))
7+
->addRulesToApply(['NoTodoCommentRule', 'LogoPresenceRule']);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Tests\Unit\Configuration;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class ResolverTest extends TestCase
9+
{
10+
#[Test]
11+
public function resolvesConfigurationFromArrayFormat(): void
12+
{
13+
$this->markTestIncomplete('Missing unit tests.');
14+
// TODO: Add tests.
15+
}
16+
}

0 commit comments

Comments
 (0)