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

keradus / PHP-CS-Fixer / 17899394524

21 Sep 2025 09:49PM UTC coverage: 94.396% (-0.2%) from 94.55%
17899394524

push

github

web-flow
refactor: introduce concept of AutomaticRuleSet (#9067)

99 of 150 new or added lines in 6 files covered. (66.0%)

4 existing lines in 2 files now uncovered.

28486 of 30177 relevant lines covered (94.4%)

45.35 hits per line

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

0.0
/src/RuleSet/AutomaticMigrationSetTrait.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\RuleSet;
16

17
use Composer\Semver\Semver;
18
use PhpCsFixer\ComposerJsonReader;
19
use PhpCsFixer\ConfigurationException\UnresolvableAutoRuleSetConfigurationException;
20

21
/**
22
 * @internal
23
 *
24
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
25
 *
26
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
27
 */
28
trait AutomaticMigrationSetTrait
29
{
30
    private function calculateTargetSet(string $setName, string $entity, bool $isRisky): string
31
    {
NEW
32
        static $set = null;
×
33

NEW
34
        if (null === $set) {
×
NEW
35
            $actualVersion = self::calculateActualVersion($setName, $entity);
×
36

NEW
37
            $candidates = self::calculateCandidateSets($entity, $isRisky);
×
NEW
38
            $composerCandidates = Semver::rsort(array_keys($candidates));
×
39

NEW
40
            foreach ($composerCandidates as $candidate) {
×
NEW
41
                if (Semver::satisfies($actualVersion, '>='.$candidate)) {
×
NEW
42
                    $set = $candidates[$candidate]; // @phpstan-ignore offsetAccess.notFound
×
43

NEW
44
                    break;
×
45
                }
46
            }
47

NEW
48
            if (null === $set) {
×
NEW
49
                throw new UnresolvableAutoRuleSetConfigurationException(\sprintf('No migration set found feasible for %s %s.', $entity, $actualVersion));
×
50
            }
51
        }
52

NEW
53
        return $set;
×
54
    }
55

56
    /**
57
     * @return list<AbstractMigrationSetDescription>
58
     */
59
    private static function getMigrationSets(): array
60
    {
NEW
61
        static $sets = null;
×
62

NEW
63
        if (null === $sets) {
×
NEW
64
            $sets = array_values(array_filter(
×
NEW
65
                RuleSets::getSetDefinitions(),
×
NEW
66
                static fn ($set) => !($set instanceof DeprecatedRuleSetDescriptionInterface) && is_subclass_of($set, AbstractMigrationSetDescription::class),
×
NEW
67
            ));
×
68
        }
69

NEW
70
        return $sets;
×
71
    }
72

73
    private static function calculateActualVersion(string $setName, string $entity): string
74
    {
NEW
75
        $composerJsonReader = ComposerJsonReader::createSingleton();
×
76

NEW
77
        if ('PHP' === $entity) {
×
NEW
78
            $version = $composerJsonReader->getPhp();
×
NEW
79
        } elseif ('PHPUnit' === $entity) {
×
NEW
80
            $version = $composerJsonReader->getPhpUnit();
×
81
        } else {
NEW
82
            throw new \InvalidArgumentException(\sprintf('Entity "%s" is not supported.', $entity));
×
83
        }
84

NEW
85
        if (null === $version) {
×
NEW
86
            throw new UnresolvableAutoRuleSetConfigurationException(\sprintf('Cannot detect %s version from "composer.json".', $entity));
×
87
        }
88

NEW
89
        return $version;
×
90
    }
91

92
    /**
93
     * @return array<string, string> [ 'major.minor' => '@SetName', ... ]
94
     */
95
    private static function calculateCandidateSets(string $entity, bool $isRisky): array
96
    {
NEW
97
        $candidates = [];
×
NEW
98
        foreach (self::getMigrationSets() as $set) {
×
NEW
99
            if ($set->getEntity() !== $entity) {
×
NEW
100
                continue;
×
101
            }
102

NEW
103
            if ($set->isRisky() !== $isRisky) {
×
NEW
104
                continue;
×
105
            }
106

NEW
107
            $candidates[$set->getVersionMajorMinor()] = $set->getName();
×
108
        }
109

NEW
110
        return $candidates;
×
111
    }
112
}
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