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

JBZoo / Codestyle / 6510687995

08 Sep 2023 10:49PM UTC coverage: 88.424%. Remained the same
6510687995

push

github

web-flow
New codestyle rules (#46)

550 of 622 relevant lines covered (88.42%)

6.86 hits per line

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

95.83
/src/PhpCsFixer/PhpCsFixerCodingStandard.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Codestyle.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Codestyle
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Codestyle\PhpCsFixer;
18

19
use PhpCsFixer\Config;
20
use PhpCsFixer\ConfigInterface;
21
use PhpCsFixerCustomFixers\Fixer\NoDuplicatedArrayKeyFixer;
22
use PhpCsFixerCustomFixers\Fixer\NoUselessCommentFixer;
23
use PhpCsFixerCustomFixers\Fixers;
24
use Symfony\Component\Finder\Finder;
25

26
/**
27
 * See details: https://mlocati.github.io/php-cs-fixer-configurator.
28
 */
29
final class PhpCsFixerCodingStandard
30
{
31
    private array $ruleSet = [
32
        // Presets
33
        '@PHP80Migration'           => true,
34
        '@PHP80Migration:risky'     => true,
35
        '@PHPUnit84Migration:risky' => true,
36
        '@Symfony'                  => true,
37
        '@Symfony:risky'            => true,
38
        '@PhpCsFixer'               => true,
39
        '@PhpCsFixer:risky'         => true,
40
        '@PSR12'                    => true,
41
        '@PSR12:risky'              => true,
42

43
        // The most dangerous options! They are excluded from above presets. Just in case.
44
        'no_trailing_whitespace_in_string'      => false,
45
        'no_unneeded_final_method'              => false,
46
        'strict_comparison'                     => false, // TODO: Check ho many issues we have in real code.
47
        'error_suppression'                     => false, // TODO: Check ho many issues we have in real code.
48
        'no_unreachable_default_argument_value' => false, // TODO: Check ho many issues we have in real code.
49
        'no_unset_on_property'                  => false, // TODO: Check ho many issues we have in real code.
50

51
        // PHPDocs & Comments
52
        'align_multiline_comment'    => ['comment_type' => 'phpdocs_like'],
53
        'phpdoc_separation'          => false,
54
        'phpdoc_to_comment'          => false,
55
        'no_superfluous_phpdoc_tags' => [
56
            'allow_mixed'         => true,
57
            'allow_unused_params' => true,
58
        ],
59
        'phpdoc_line_span' => [
60
            'const'    => 'single',
61
            'property' => 'single',
62
            'method'   => 'multi',
63
        ],
64

65
        // PHPUnit
66
        'php_unit_internal_class'                => false,
67
        'php_unit_strict'                        => false,
68
        'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
69
        'php_unit_test_class_requires_covers'    => false,
70

71
        // Import & ordering rules
72
        'no_unused_imports'                => true,
73
        'blank_line_between_import_groups' => true,
74
        'fully_qualified_strict_types'     => true,
75
        'global_namespace_import'          => [
76
            'import_classes'   => false,
77
            'import_constants' => false,
78
            'import_functions' => false,
79
        ],
80
        'ordered_imports' => [
81
            'sort_algorithm' => 'alpha',
82
            'imports_order'  => ['class', 'function', 'const'],
83
        ],
84
        'ordered_interfaces'     => ['direction' => 'ascend', 'order' => 'alpha'],
85
        'ordered_class_elements' => [
86
            'order' => [
87
                'use_trait',
88
                'case',
89
                // Const
90
                'constant',
91
                'constant_public',
92
                'constant_protected',
93
                'constant_private',
94
                // Props
95
                'property',
96
                'property_public_static',
97
                'property_protected_static',
98
                'property_private_static',
99
                'property_public',
100
                'property_protected',
101
                'property_private',
102
                // Statics
103
                'method_public_abstract_static',
104
                'method_protected_abstract_static',
105
                'method_public_abstract',
106
                'method_protected_abstract',
107
                // Magic methods
108
                'construct',
109
                'destruct',
110
                'magic',
111
                'phpunit',
112
                // Regular Methods
113
                'method',
114
                'method_public',
115
                'method_public_static',
116
                'method_protected',
117
                'method_protected_static',
118
                'method_private',
119
                'method_private_static',
120
            ],
121
        ],
122

123
        'binary_operator_spaces' => [
124
            'operators' => [
125
                '='  => 'align_single_space_minimal',
126
                '=>' => 'align_single_space_minimal',
127
            ],
128
        ],
129

130
        'type_declaration_spaces' => ['elements' => ['function']],
131

132
        // Blank lines & spaces
133
        'cast_spaces'  => ['space' => 'none'],
134
        'types_spaces' => ['space' => 'none'],
135
        'concat_space' => ['spacing' => 'one'],
136

137
        'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'],
138
        'trailing_comma_in_multiline'            => [
139
            'after_heredoc' => true,
140
            'elements'      => ['arrays', 'arguments', 'parameters'],
141
        ],
142

143
        'blank_line_before_statement' => [
144
            'statements' => [
145
                'case',
146
                'default',
147
                'declare',
148
                'do',
149
                'for',
150
                'foreach',
151
                'return',
152
                'switch',
153
                'try',
154
                'while',
155
                'phpdoc',
156
            ],
157
        ],
158

159
        'no_extra_blank_lines' => [
160
            'tokens' => [
161
                'attribute',
162
                'break',
163
                'case',
164
                'continue',
165
                'curly_brace_block',
166
                'default',
167
                'extra',
168
                'parenthesis_brace_block',
169
                'return',
170
                'square_brace_block',
171
                'switch',
172
                'throw',
173
                'use',
174
            ],
175
        ],
176

177
        'single_space_around_construct'           => true,
178
        'control_structure_braces'                => true,
179
        'curly_braces_position'                   => true,
180
        'control_structure_continuation_position' => true,
181
        'declare_parentheses'                     => true,
182
        'statement_indentation'                   => true,
183
        'no_multiple_statements_per_line'         => true,
184
        'method_chaining_indentation'             => true,
185

186
        // Extra rules
187
        'increment_style' => ['style' => 'post'],
188
        'yoda_style'      => [
189
            'equal'            => true,
190
            'identical'        => false,
191
            'less_and_greater' => false,
192
        ],
193
        'regular_callable_call'      => true,
194
        'self_static_accessor'       => true,
195
        'simplified_if_return'       => true,
196
        'static_lambda'              => true,
197
        'native_function_invocation' => [
198
            'scope'   => 'all',
199
            'strict'  => true,
200
            'include' => ['@internal'],
201
        ],
202
        'native_constant_invocation' => [
203
            'fix_built_in' => true,
204
            'include'      => [
205
                'DIRECTORY_SEPARATOR',
206
                'PHP_INT_SIZE',
207
                'PHP_SAPI',
208
                'PHP_VERSION_ID',
209
            ],
210
            'scope'  => 'all',
211
            'strict' => true,
212
        ],
213

214
        'nullable_type_declaration_for_default_null_value' => true,
215

216
        'single_line_empty_body' => false,
217

218
        // For the future
219
        // 'final_class' => true
220
    ];
221

222
    private string $projectPath;
223
    private string $styleName = 'JBZooStyle';
224

225
    public function __construct(string $projectPath)
226
    {
227
        $path = \realpath($projectPath);
8✔
228

229
        if ($path === false) {
8✔
230
            throw new \RuntimeException("Project path \"{$projectPath}\" is not valid");
×
231
        }
232

233
        $this->projectPath = $path;
8✔
234
    }
235

236
    public function getRules(): array
237
    {
238
        return $this->ruleSet;
8✔
239
    }
240

241
    public function getFixerConfig(?Finder $customFinder = null, array $customRules = []): ConfigInterface
242
    {
243
        $customRules = [
8✔
244
            NoDuplicatedArrayKeyFixer::name() => true,
8✔
245
            NoUselessCommentFixer::name()     => true,
8✔
246
        ] + $customRules;
4✔
247

248
        return (new Config($this->styleName))
8✔
249
            ->setRiskyAllowed(true)
8✔
250
            ->registerCustomFixers(new Fixers())
8✔
251
            ->setCacheFile("{$this->projectPath}/build/php-cs-fixer-cache.json")
8✔
252
            ->setFinder($customFinder ?? $this->getFinder())
8✔
253
            ->setRules($this->getRules() + $customRules);
8✔
254
    }
255

256
    public function getFinder(): Finder
257
    {
258
        return (new Finder())
8✔
259
            ->files()
8✔
260
            ->followLinks()
8✔
261
            ->ignoreVCS(true)
8✔
262
            ->ignoreDotFiles(false)
8✔
263
            ->in($this->projectPath)
8✔
264
            ->exclude('vendor')
8✔
265
            ->exclude('build')
8✔
266
            ->name('/\.php$/');
8✔
267
    }
268
}
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