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

PHP-CS-Fixer / PHP-CS-Fixer / 3721300657

pending completion
3721300657

push

github

GitHub
minor: Follow PSR12 ordered imports in Symfony ruleset (#6712)

9 of 9 new or added lines in 2 files covered. (100.0%)

22674 of 24281 relevant lines covered (93.38%)

39.08 hits per line

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

34.92
/src/AbstractFixer.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;
16

17
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
18
use PhpCsFixer\ConfigurationException\InvalidForEnvFixerConfigurationException;
19
use PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException;
20
use PhpCsFixer\Console\Application;
21
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
22
use PhpCsFixer\Fixer\FixerInterface;
23
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
24
use PhpCsFixer\FixerConfiguration\DeprecatedFixerOption;
25
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
26
use PhpCsFixer\FixerConfiguration\InvalidOptionsForEnvException;
27
use PhpCsFixer\Tokenizer\Tokens;
28
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
29
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
30

31
/**
32
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
33
 *
34
 * @internal
35
 */
36
abstract class AbstractFixer implements FixerInterface
37
{
38
    /**
39
     * @var null|array<string, mixed>
40
     */
41
    protected $configuration;
42

43
    /**
44
     * @var WhitespacesFixerConfig
45
     */
46
    protected $whitespacesConfig;
47

48
    /**
49
     * @var null|FixerConfigurationResolverInterface
50
     */
51
    private $configurationDefinition;
52

53
    public function __construct()
54
    {
55
        if ($this instanceof ConfigurableFixerInterface) {
6✔
56
            try {
57
                $this->configure([]);
×
58
            } catch (RequiredFixerConfigurationException $e) {
×
59
                // ignore
60
            }
61
        }
62

63
        if ($this instanceof WhitespacesAwareFixerInterface) {
6✔
64
            $this->whitespacesConfig = $this->getDefaultWhitespacesFixerConfig();
1✔
65
        }
66
    }
67

68
    final public function fix(\SplFileInfo $file, Tokens $tokens): void
69
    {
70
        if ($this instanceof ConfigurableFixerInterface && null === $this->configuration) {
×
71
            throw new RequiredFixerConfigurationException($this->getName(), 'Configuration is required.');
×
72
        }
73

74
        if (0 < $tokens->count() && $this->isCandidate($tokens) && $this->supports($file)) {
×
75
            $this->applyFix($file, $tokens);
×
76
        }
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function isRisky(): bool
83
    {
84
        return false;
1✔
85
    }
86

87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getName(): string
91
    {
92
        $nameParts = explode('\\', static::class);
1✔
93
        $name = substr(end($nameParts), 0, -\strlen('Fixer'));
1✔
94

95
        return Utils::camelCaseToUnderscore($name);
1✔
96
    }
97

98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getPriority(): int
102
    {
103
        return 0;
1✔
104
    }
105

106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function supports(\SplFileInfo $file): bool
110
    {
111
        return true;
1✔
112
    }
113

114
    /**
115
     * @param array<string, mixed> $configuration
116
     */
117
    public function configure(array $configuration): void
118
    {
119
        if (!$this instanceof ConfigurableFixerInterface) {
1✔
120
            throw new \LogicException('Cannot configure using Abstract parent, child not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".');
1✔
121
        }
122

123
        foreach ($this->getConfigurationDefinition()->getOptions() as $option) {
×
124
            if (!$option instanceof DeprecatedFixerOption) {
×
125
                continue;
×
126
            }
127

128
            $name = $option->getName();
×
129
            if (\array_key_exists($name, $configuration)) {
×
130
                Utils::triggerDeprecation(new \InvalidArgumentException(sprintf(
×
131
                    'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
×
132
                    $name,
×
133
                    $this->getName(),
×
134
                    Application::getMajorVersion() + 1,
×
135
                    str_replace('`', '"', $option->getDeprecationMessage())
×
136
                )));
×
137
            }
138
        }
139

140
        try {
141
            $this->configuration = $this->getConfigurationDefinition()->resolve($configuration);
×
142
        } catch (MissingOptionsException $exception) {
×
143
            throw new RequiredFixerConfigurationException(
×
144
                $this->getName(),
×
145
                sprintf('Missing required configuration: %s', $exception->getMessage()),
×
146
                $exception
×
147
            );
×
148
        } catch (InvalidOptionsForEnvException $exception) {
×
149
            throw new InvalidForEnvFixerConfigurationException(
×
150
                $this->getName(),
×
151
                sprintf('Invalid configuration for env: %s', $exception->getMessage()),
×
152
                $exception
×
153
            );
×
154
        } catch (ExceptionInterface $exception) {
×
155
            throw new InvalidFixerConfigurationException(
×
156
                $this->getName(),
×
157
                sprintf('Invalid configuration: %s', $exception->getMessage()),
×
158
                $exception
×
159
            );
×
160
        }
161
    }
162

163
    public function getConfigurationDefinition(): FixerConfigurationResolverInterface
164
    {
165
        if (!$this instanceof ConfigurableFixerInterface) {
1✔
166
            throw new \LogicException(sprintf('Cannot get configuration definition using Abstract parent, child "%s" not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".', static::class));
1✔
167
        }
168

169
        if (null === $this->configurationDefinition) {
×
170
            $this->configurationDefinition = $this->createConfigurationDefinition();
×
171
        }
172

173
        return $this->configurationDefinition;
×
174
    }
175

176
    public function setWhitespacesConfig(WhitespacesFixerConfig $config): void
177
    {
178
        if (!$this instanceof WhitespacesAwareFixerInterface) {
2✔
179
            throw new \LogicException('Cannot run method for class not implementing "PhpCsFixer\Fixer\WhitespacesAwareFixerInterface".');
1✔
180
        }
181

182
        $this->whitespacesConfig = $config;
1✔
183
    }
184

185
    abstract protected function applyFix(\SplFileInfo $file, Tokens $tokens): void;
186

187
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
188
    {
189
        if (!$this instanceof ConfigurableFixerInterface) {
1✔
190
            throw new \LogicException('Cannot create configuration definition using Abstract parent, child not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".');
1✔
191
        }
192

193
        throw new \LogicException('Not implemented.');
×
194
    }
195

196
    private function getDefaultWhitespacesFixerConfig(): WhitespacesFixerConfig
197
    {
198
        static $defaultWhitespacesFixerConfig = null;
1✔
199

200
        if (null === $defaultWhitespacesFixerConfig) {
1✔
201
            $defaultWhitespacesFixerConfig = new WhitespacesFixerConfig('    ', "\n");
1✔
202
        }
203

204
        return $defaultWhitespacesFixerConfig;
1✔
205
    }
206
}
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

© 2025 Coveralls, Inc