Skip to content

Commit

Permalink
Add support for config files with .php extension (#247)
Browse files Browse the repository at this point in the history
* Add support for config files with .php extension

* Mention config files with .php extensions in README

* Use config files with .php extensions in README examples

* Remove unused namespaced_loaders test data

* Make tests use config files with .php extension

* Fix: Run 'make static-code-analysis-baseline'

* Fix: Adjust tests

* Fix: Give non-distributable configuration files priority

Co-authored-by: Andreas Möller <am@localheinz.com>
  • Loading branch information
yguedidi and localheinz committed May 20, 2022
1 parent b3643c5 commit 6e4c196
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 36 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ For more complex needs, have a look at the [custom ruleset documentation](doc/ru
Using configuration, you can easily store per-project settings:

```php
// ~/.twig_cs.dist
// ~/.twig_cs.dist.php
<?php

return \FriendsOfTwig\Twigcs\Config\Config::create()
Expand All @@ -140,15 +140,22 @@ you must use the `--config` option:

```
cd ~/dirA
twigcs --config ~/dirB/.twig_cs.dist # Will lint templates in ~/dirA with the config of ~/dirB
twigcs --config ~/dirB/.twig_cs.dist.php # Will lint templates in ~/dirA with the config of ~/dirB
```

By default, the files `.twig_cs` and `.twig_cs.dist` are looked up in your current working directory (CWD).
By default, the files

- `.twig_cs.php`
- `.twig_cs`
- `.twig_cs.dist.php`
- `.twig_cs.dist`

are looked up in your current working directory (CWD).

You can also provide finders inside config files, they will completely replace the path in the CLI:

```php
// ~/.twig_cs.dist
// ~/.twig_cs.dist.php
<?php

$finderA = FriendsOfTwig\Twigcs\Finder\TemplateFinder::create()->in(__DIR__.'/dirA');
Expand Down
4 changes: 3 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
<MixedInferredReturnType occurrences="1">
<code>string</code>
</MixedInferredReturnType>
<MixedOperand occurrences="3">
<MixedOperand occurrences="5">
<code>$configDir</code>
<code>$configDir</code>
<code>$configDir</code>
<code>$configDir</code>
<code>$cwd</code>
Expand Down
2 changes: 2 additions & 0 deletions src/Config/ConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ private function computeConfigFiles()
$configDir = $this->cwd;

return [
$configDir.\DIRECTORY_SEPARATOR.'.twig_cs.php',
$configDir.\DIRECTORY_SEPARATOR.'.twig_cs',
$configDir.\DIRECTORY_SEPARATOR.'.twig_cs.dist.php',
$configDir.\DIRECTORY_SEPARATOR.'.twig_cs.dist',
];
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Config/ConfigResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,34 @@ public function testGetConfigFileReturnsConfigFileWhenConfigOptionHasNotBeenSpec
public function provideDefaultConfigFileNameAndAvailableDefaultConfigFileNames(): array
{
return [
[
'.twig_cs.php',
[
'.twig_cs',
'.twig_cs.dist',
'.twig_cs.dist.php',
'.twig_cs.php',
],
],
[
'.twig_cs',
[
'.twig_cs',
'.twig_cs.dist',
'.twig_cs.dist.php',
],
],
[
'.twig_cs.dist.php',
[
'.twig_cs.dist',
'.twig_cs.dist.php',
],
],
[
'.twig_cs.dist',
[
'.twig_cs.dist',
],
],
];
Expand Down Expand Up @@ -278,6 +301,8 @@ public function provideDefaultConfigFileName(): \Generator
$defaultConfigFileNames = [
'.twig_cs',
'.twig_cs.dist',
'.twig_cs.dist.php',
'.twig_cs.php',
];

foreach ($defaultConfigFileNames as $defaultConfigFileName) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Console/LintCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testConfigFileWithoutCliPath()
{
$this->commandTester->execute([
'paths' => null,
'--config' => 'tests/data/config/external/.twig_cs.dist',
'--config' => 'tests/data/config/external/.twig_cs.dist.php',
]);

$output = $this->commandTester->getDisplay();
Expand All @@ -209,7 +209,7 @@ public function testConfigFileWithCliPath()
{
$this->commandTester->execute([
'paths' => ['tests/data/syntax_error'],
'--config' => 'tests/data/config/external/.twig_cs.dist',
'--config' => 'tests/data/config/external/.twig_cs.dist.php',
]);

$output = $this->commandTester->getDisplay();
Expand All @@ -227,7 +227,7 @@ public function testConfigFileWithCliPath()
public function testConfigFileWithDisplayAndSeverity()
{
$this->commandTester->execute([
'--config' => 'tests/data/config/external/.twig_cs_with_display_blocking.dist',
'--config' => 'tests/data/config/external/.twig_cs_with_display_blocking.dist.php',
]);

$output = $this->commandTester->getDisplay();
Expand Down Expand Up @@ -271,7 +271,7 @@ public function testConfigFileSamePathWithRulesetOverrides()
public function testUnusedWithFileLoader()
{
$this->commandTester->execute([
'--config' => 'tests/data/config/loaders/.twig_cs.dist',
'--config' => 'tests/data/config/loaders/.twig_cs.dist.php',
]);

$output = $this->commandTester->getDisplay();
Expand Down
File renamed without changes.
File renamed without changes.
20 changes: 0 additions & 20 deletions tests/data/config/namespaced_loaders/.twig_cs.dist

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/config/namespaced_loaders/a/child.html.twig

This file was deleted.

4 changes: 0 additions & 4 deletions tests/data/config/namespaced_loaders/b/parent.html.twig

This file was deleted.

0 comments on commit 6e4c196

Please sign in to comment.