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

NexusPHP / cs-config / 14655342589

23 Feb 2025 03:16PM UTC coverage: 95.644%. Remained the same
14655342589

push

github

paulbalandan
Update build badge

1076 of 1125 relevant lines covered (95.64%)

63.51 hits per line

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

100.0
/src/Factory.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Nexus CS Config.
7
 *
8
 * (c) 2020 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Nexus\CsConfig;
15

16
use Nexus\CsConfig\Ruleset\RulesetInterface;
17
use PhpCsFixer\Config;
18
use PhpCsFixer\ConfigInterface;
19
use PhpCsFixer\Finder;
20
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
21

22
/**
23
 * The Factory class is invoked on each project's `.php-cs-fixer.dist.php` to create
24
 * the specific ruleset for the project.
25
 */
26
final class Factory
27
{
28
    /**
29
     * @param array{
30
     *     cacheFile: non-empty-string,
31
     *     customFixers: iterable<\PhpCsFixer\Fixer\FixerInterface>,
32
     *     finder: \PhpCsFixer\Finder|iterable<\SplFileInfo>,
33
     *     format: string,
34
     *     hideProgress: bool,
35
     *     indent: non-empty-string,
36
     *     lineEnding: non-empty-string,
37
     *     isRiskyAllowed: bool,
38
     *     usingCache: bool,
39
     *     rules: array<string, array<string, mixed>|bool>
40
     * } $options Array of resolved options
41
     */
42
    private function __construct(private RulesetInterface $ruleset, private array $options) {}
43

44
    /**
45
     * Prepares the ruleset and options before the `PhpCsFixer\Config` object
46
     * is created.
47
     *
48
     * @param array<string, array<string, mixed>|bool> $overrides
49
     * @param array{
50
     *     cacheFile?: non-empty-string,
51
     *     customFixers?: iterable<\PhpCsFixer\Fixer\FixerInterface>,
52
     *     finder?: \PhpCsFixer\Finder|iterable<\SplFileInfo>,
53
     *     format?: string,
54
     *     hideProgress?: bool,
55
     *     indent?: non-empty-string,
56
     *     lineEnding?: non-empty-string,
57
     *     isRiskyAllowed?: bool,
58
     *     usingCache?: bool,
59
     *     customRules?: array<string, array<string, mixed>|bool>
60
     * } $options
61
     */
62
    public static function create(RulesetInterface $ruleset, array $overrides = [], array $options = []): self
63
    {
64
        if (\PHP_VERSION_ID < $ruleset->getRequiredPHPVersion()) {
28✔
65
            throw new \RuntimeException(\sprintf(
4✔
66
                'The "%s" ruleset requires a minimum PHP_VERSION_ID of "%d" but current PHP_VERSION_ID is "%d".',
4✔
67
                $ruleset->getName(),
4✔
68
                $ruleset->getRequiredPHPVersion(),
4✔
69
                \PHP_VERSION_ID,
4✔
70
            ));
4✔
71
        }
72

73
        // Meant to be used in vendor/ to get to the root directory
74
        $dir = \dirname(__DIR__, 4);
24✔
75
        $dir = (string) realpath($dir);
24✔
76

77
        $defaultFinder = Finder::create()
24✔
78
            ->files()
24✔
79
            ->in([$dir])
24✔
80
            ->exclude(['build'])
24✔
81
        ;
24✔
82

83
        // Resolve Config options
84
        $options['cacheFile'] ??= '.php-cs-fixer.cache';
24✔
85
        $options['customFixers'] ??= [];
24✔
86
        $options['finder'] ??= $defaultFinder;
24✔
87
        $options['format'] ??= 'txt';
24✔
88
        $options['hideProgress'] ??= false;
24✔
89
        $options['indent'] ??= '    ';
24✔
90
        $options['lineEnding'] ??= "\n";
24✔
91
        $options['isRiskyAllowed'] ??= $ruleset->willAutoActivateIsRiskyAllowed();
24✔
92
        $options['usingCache'] ??= true;
24✔
93
        $options['rules'] = array_merge($ruleset->getRules(), $overrides, $options['customRules'] ?? []);
24✔
94

95
        return new self($ruleset, $options);
24✔
96
    }
97

98
    /**
99
     * Creates a `PhpCsFixer\Config` object that is applicable for libraries,
100
     * i.e., has their own header docblock in place.
101
     */
102
    public function forLibrary(string $library, string $author, string $email = '', ?int $startingYear = null): ConfigInterface
103
    {
104
        $year = (string) $startingYear;
8✔
105

106
        if ('' !== $year) {
8✔
107
            $year .= ' ';
8✔
108
        }
109

110
        if ('' !== $email) {
8✔
111
            $email = trim($email, '<>');
8✔
112
            $email = ' <'.$email.'>';
8✔
113
        }
114

115
        $header = \sprintf(
8✔
116
            <<<'HEADER'
8✔
117
                This file is part of %s.
118

119
                (c) %s%s%s
120

121
                For the full copyright and license information, please view
122
                the LICENSE file that was distributed with this source code.
123
                HEADER,
8✔
124
            $library,
8✔
125
            $year,
8✔
126
            $author,
8✔
127
            $email,
8✔
128
        );
8✔
129

130
        return $this->invoke([
8✔
131
            'header_comment' => [
8✔
132
                'header' => trim($header),
8✔
133
                'comment_type' => 'PHPDoc',
8✔
134
                'location' => 'after_declare_strict',
8✔
135
                'separate' => 'both',
8✔
136
            ],
8✔
137
        ]);
8✔
138
    }
139

140
    /**
141
     * Plain invocation of `Config` with no additional arguments.
142
     */
143
    public function forProjects(): ConfigInterface
144
    {
145
        return $this->invoke();
20✔
146
    }
147

148
    /**
149
     * The main method of creating the Config instance.
150
     *
151
     * @param array<string, array<string, mixed>|bool> $overrides
152
     *
153
     * @internal
154
     */
155
    private function invoke(array $overrides = []): ConfigInterface
156
    {
157
        $rules = array_merge($this->options['rules'], $overrides);
24✔
158

159
        return (new Config($this->ruleset->getName()))
24✔
160
            ->setParallelConfig(ParallelConfigFactory::detect())
24✔
161
            ->registerCustomFixers($this->options['customFixers'])
24✔
162
            ->setCacheFile($this->options['cacheFile'])
24✔
163
            ->setFinder($this->options['finder'])
24✔
164
            ->setFormat($this->options['format'])
24✔
165
            ->setHideProgress($this->options['hideProgress'])
24✔
166
            ->setIndent($this->options['indent'])
24✔
167
            ->setLineEnding($this->options['lineEnding'])
24✔
168
            ->setRiskyAllowed($this->options['isRiskyAllowed'])
24✔
169
            ->setUsingCache($this->options['usingCache'])
24✔
170
            ->setRules($rules)
24✔
171
        ;
24✔
172
    }
173
}
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