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

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

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

90.0
/system/Commands/Utilities/FilterCheck.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.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 CodeIgniter\Commands\Utilities;
15

16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
18
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
19

20
/**
21
 * Check filters for a route.
22
 */
23
class FilterCheck extends BaseCommand
24
{
25
    /**
26
     * The group the command is lumped under
27
     * when listing commands.
28
     *
29
     * @var string
30
     */
31
    protected $group = 'CodeIgniter';
32

33
    /**
34
     * The Command's name
35
     *
36
     * @var string
37
     */
38
    protected $name = 'filter:check';
39

40
    /**
41
     * the Command's short description
42
     *
43
     * @var string
44
     */
45
    protected $description = 'Check filters for a route.';
46

47
    /**
48
     * the Command's usage
49
     *
50
     * @var string
51
     */
52
    protected $usage = 'filter:check <HTTP method> <route>';
53

54
    /**
55
     * the Command's Arguments
56
     *
57
     * @var array<string, string>
58
     */
59
    protected $arguments = [
60
        'method' => 'The HTTP method. GET, POST, PUT, etc.',
61
        'route'  => 'The route (URI path) to check filters.',
62
    ];
63

64
    /**
65
     * the Command's Options
66
     *
67
     * @var array<string, string>
68
     */
69
    protected $options = [];
70

71
    /**
72
     * @return int exit code
73
     */
74
    public function run(array $params)
75
    {
76
        if (! $this->checkParams($params)) {
2✔
77
            return EXIT_ERROR;
×
78
        }
79

80
        $method = $params[0];
2✔
81
        $route  = $params[1];
2✔
82

83
        // Load Routes
84
        service('routes')->loadRoutes();
2✔
85

86
        $filterCollector = new FilterCollector();
2✔
87

88
        $filters = $filterCollector->get($method, $route);
2✔
89

90
        // PageNotFoundException
91
        if ($filters['before'] === ['<unknown>']) {
2✔
92
            CLI::error(
1✔
93
                "Can't find a route: " .
1✔
94
                CLI::color(
1✔
95
                    '"' . strtoupper($method) . ' ' . $route . '"',
1✔
96
                    'black',
1✔
97
                    'light_gray'
1✔
98
                ),
1✔
99
            );
1✔
100

101
            return EXIT_ERROR;
1✔
102
        }
103

104
        $this->showTable($filterCollector, $filters, $method, $route);
1✔
105
        $this->showFilterClasses($filterCollector, $method, $route);
1✔
106

107
        return EXIT_SUCCESS;
1✔
108
    }
109

110
    /**
111
     * @param array<int|string, string|null> $params
112
     */
113
    private function checkParams(array $params): bool
114
    {
115
        if (! isset($params[0], $params[1])) {
2✔
116
            CLI::error('You must specify a HTTP verb and a route.');
×
117
            CLI::write('  Usage: ' . $this->usage);
×
118
            CLI::write('Example: filter:check GET /');
×
119
            CLI::write('         filter:check PUT products/1');
×
120

121
            return false;
×
122
        }
123

124
        return true;
2✔
125
    }
126

127
    /**
128
     * @param array{before: list<string>, after: list<string>} $filters
129
     */
130
    private function showTable(
131
        FilterCollector $filterCollector,
132
        array $filters,
133
        string $method,
134
        string $route
135
    ): void {
136
        $thead = [
1✔
137
            'Method',
1✔
138
            'Route',
1✔
139
            'Before Filters',
1✔
140
            'After Filters',
1✔
141
        ];
1✔
142

143
        $required = $filterCollector->getRequiredFilters();
1✔
144

145
        $coloredRequired = $this->colorItems($required);
1✔
146

147
        $before = array_merge($coloredRequired['before'], $filters['before']);
1✔
148
        $after  = array_merge($filters['after'], $coloredRequired['after']);
1✔
149

150
        $tbody   = [];
1✔
151
        $tbody[] = [
1✔
152
            strtoupper($method),
1✔
153
            $route,
1✔
154
            implode(' ', $before),
1✔
155
            implode(' ', $after),
1✔
156
        ];
1✔
157

158
        CLI::table($tbody, $thead);
1✔
159
    }
160

161
    /**
162
     * Color all elements of the array.
163
     *
164
     * @param array<array-key, mixed> $array
165
     *
166
     * @return array<array-key, mixed>
167
     */
168
    private function colorItems(array $array): array
169
    {
170
        return array_map(function ($item): array|string {
1✔
171
            if (is_array($item)) {
1✔
172
                return $this->colorItems($item);
1✔
173
            }
174

175
            return CLI::color($item, 'yellow');
1✔
176
        }, $array);
1✔
177
    }
178

179
    private function showFilterClasses(
180
        FilterCollector $filterCollector,
181
        string $method,
182
        string $route
183
    ): void {
184
        $requiredFilterClasses = $filterCollector->getRequiredFilterClasses();
1✔
185
        $filterClasses         = $filterCollector->getClasses($method, $route);
1✔
186

187
        $coloredRequiredFilterClasses = $this->colorItems($requiredFilterClasses);
1✔
188

189
        $classList = [
1✔
190
            'before' => array_merge($coloredRequiredFilterClasses['before'], $filterClasses['before']),
1✔
191
            'after'  => array_merge($filterClasses['after'], $coloredRequiredFilterClasses['after']),
1✔
192
        ];
1✔
193

194
        foreach ($classList as $position => $classes) {
1✔
195
            CLI::write(ucfirst($position) . ' Filter Classes:', 'cyan');
1✔
196
            CLI::write(implode(' → ', $classes));
1✔
197
        }
198
    }
199
}
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