• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 17253322895

26 Aug 2025 11:52PM UTC coverage: 94.753% (+0.008%) from 94.745%
17253322895

push

github

keradus
add to git-blame-ignore-revs

28316 of 29884 relevant lines covered (94.75%)

45.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.84
/src/Fixer/ConfigurableFixerTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Fixer;
16

17
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
18
use PhpCsFixer\ConfigurationException\InvalidForEnvFixerConfigurationException;
19
use PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException;
20
use PhpCsFixer\Console\Application;
21
use PhpCsFixer\FixerConfiguration\DeprecatedFixerOption;
22
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
23
use PhpCsFixer\FixerConfiguration\InvalidOptionsForEnvException;
24
use PhpCsFixer\Utils;
25
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
26
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
27

28
/**
29
 * @internal
30
 *
31
 * @template TFixerInputConfig of array<string, mixed>
32
 * @template TFixerComputedConfig of array<string, mixed>
33
 *
34
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
35
 *
36
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
37
 */
38
trait ConfigurableFixerTrait
39
{
40
    /**
41
     * @var null|TFixerComputedConfig
42
     */
43
    protected ?array $configuration = null;
44

45
    private ?FixerConfigurationResolverInterface $configurationDefinition = null;
46

47
    /**
48
     * @param TFixerInputConfig $configuration
49
     */
50
    final public function configure(array $configuration): void
51
    {
52
        $this->configurePreNormalisation($configuration);
13,421✔
53

54
        foreach ($this->getConfigurationDefinition()->getOptions() as $option) {
13,421✔
55
            if (!$option instanceof DeprecatedFixerOption) {
13,421✔
56
                continue;
13,209✔
57
            }
58

59
            $name = $option->getName();
260✔
60
            if (\array_key_exists($name, $configuration)) {
260✔
61
                Utils::triggerDeprecation(new \InvalidArgumentException(\sprintf(
94✔
62
                    'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
94✔
63
                    $name,
94✔
64
                    $this->getName(),
94✔
65
                    Application::getMajorVersion() + 1,
94✔
66
                    str_replace('`', '"', $option->getDeprecationMessage())
94✔
67
                )));
94✔
68
            }
69
        }
70

71
        try {
72
            $this->configuration = $this->getConfigurationDefinition()->resolve($configuration); // @phpstan-ignore-line ->configuration typehint is autogenerated base on ConfigurationDefinition
13,421✔
73
        } catch (MissingOptionsException $exception) {
239✔
74
            throw new RequiredFixerConfigurationException(
76✔
75
                $this->getName(),
76✔
76
                \sprintf('Missing required configuration: %s', $exception->getMessage()),
76✔
77
                $exception
76✔
78
            );
76✔
79
        } catch (InvalidOptionsForEnvException $exception) {
170✔
80
            throw new InvalidForEnvFixerConfigurationException(
×
81
                $this->getName(),
×
82
                \sprintf('Invalid configuration for env: %s', $exception->getMessage()),
×
83
                $exception
×
84
            );
×
85
        } catch (ExceptionInterface $exception) {
170✔
86
            throw new InvalidFixerConfigurationException(
162✔
87
                $this->getName(),
162✔
88
                \sprintf('Invalid configuration: %s', $exception->getMessage()),
162✔
89
                $exception
162✔
90
            );
162✔
91
        }
92

93
        $this->configurePostNormalisation();
13,403✔
94
    }
95

96
    final public function getConfigurationDefinition(): FixerConfigurationResolverInterface
97
    {
98
        if (null === $this->configurationDefinition) {
13,421✔
99
            $this->configurationDefinition = $this->createConfigurationDefinition();
13,421✔
100
        }
101

102
        return $this->configurationDefinition;
13,421✔
103
    }
104

105
    abstract public function getName(): string;
106

107
    /**
108
     * One can override me.
109
     *
110
     * @param TFixerInputConfig $configuration
111
     */
112
    protected function configurePreNormalisation(array &$configuration): void
113
    {
114
        // 🤔 ideally this method won't be needed, maybe we can remove it over time
115
    }
13,332✔
116

117
    /**
118
     * One can override me.
119
     */
120
    protected function configurePostNormalisation(): void
121
    {
122
        // 🤔 ideally this method won't be needed, maybe we can remove it over time
123
    }
6,951✔
124

125
    abstract protected function createConfigurationDefinition(): FixerConfigurationResolverInterface;
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc