Skip to content

Commit f420cc7

Browse files
authored
Merge pull request #8975 from kenjis/refactor-Validation-rules
[4.6] refactor: Validation rules and tests
2 parents d8ac60a + b6c0a66 commit f420cc7

File tree

7 files changed

+316
-1811
lines changed

7 files changed

+316
-1811
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9901,12 +9901,6 @@
99019901
'count' => 1,
99029902
'path' => __DIR__ . '/system/Validation/DotArrayFilter.php',
99039903
];
9904-
$ignoreErrors[] = [
9905-
// identifier: booleanNot.exprNotBoolean
9906-
'message' => '#^Only booleans are allowed in a negated boolean, array\\|null given\\.$#',
9907-
'count' => 6,
9908-
'path' => __DIR__ . '/system/Validation/FileRules.php',
9909-
];
99109904
$ignoreErrors[] = [
99119905
// identifier: missingType.iterableValue
99129906
'message' => '#^Method CodeIgniter\\\\Validation\\\\Rules\\:\\:differs\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#',
@@ -17881,24 +17875,6 @@
1788117875
'count' => 1,
1788217876
'path' => __DIR__ . '/tests/system/Throttle/ThrottleTest.php',
1788317877
];
17884-
$ignoreErrors[] = [
17885-
// identifier: missingType.iterableValue
17886-
'message' => '#^Method CodeIgniter\\\\Validation\\\\CreditCardRulesTest\\:\\:calculateLuhnChecksum\\(\\) has parameter \\$digits with no value type specified in iterable type array\\.$#',
17887-
'count' => 1,
17888-
'path' => __DIR__ . '/tests/system/Validation/CreditCardRulesTest.php',
17889-
];
17890-
$ignoreErrors[] = [
17891-
// identifier: missingType.iterableValue
17892-
'message' => '#^Method CodeIgniter\\\\Validation\\\\CreditCardRulesTest\\:\\:provideValidCCNumber\\(\\) return type has no value type specified in iterable type iterable\\.$#',
17893-
'count' => 1,
17894-
'path' => __DIR__ . '/tests/system/Validation/CreditCardRulesTest.php',
17895-
];
17896-
$ignoreErrors[] = [
17897-
// identifier: argument.type
17898-
'message' => '#^Parameter \\#1 \\$config of class CodeIgniter\\\\Validation\\\\Validation constructor expects Config\\\\Validation, stdClass given\\.$#',
17899-
'count' => 1,
17900-
'path' => __DIR__ . '/tests/system/Validation/CreditCardRulesTest.php',
17901-
];
1790217878
$ignoreErrors[] = [
1790317879
// identifier: missingType.iterableValue
1790417880
'message' => '#^Property CodeIgniter\\\\Validation\\\\CreditCardRulesTest\\:\\:\\$config type has no value type specified in iterable type array\\.$#',
@@ -17917,12 +17893,6 @@
1791717893
'count' => 1,
1791817894
'path' => __DIR__ . '/tests/system/Validation/DatabaseRelatedRulesTest.php',
1791917895
];
17920-
$ignoreErrors[] = [
17921-
// identifier: argument.type
17922-
'message' => '#^Parameter \\#1 \\$config of class CodeIgniter\\\\Validation\\\\Validation constructor expects Config\\\\Validation, stdClass given\\.$#',
17923-
'count' => 1,
17924-
'path' => __DIR__ . '/tests/system/Validation/FileRulesTest.php',
17925-
];
1792617896
$ignoreErrors[] = [
1792717897
// identifier: missingType.iterableValue
1792817898
'message' => '#^Property CodeIgniter\\\\Validation\\\\FileRulesTest\\:\\:\\$config type has no value type specified in iterable type array\\.$#',

system/Validation/FileRules.php

Lines changed: 2 additions & 288 deletions
Original file line numberDiff line numberDiff line change
@@ -13,299 +13,13 @@
1313

1414
namespace CodeIgniter\Validation;
1515

16-
use CodeIgniter\Exceptions\InvalidArgumentException;
17-
use CodeIgniter\HTTP\CLIRequest;
18-
use CodeIgniter\HTTP\IncomingRequest;
19-
use CodeIgniter\HTTP\RequestInterface;
20-
use Config\Mimes;
16+
use CodeIgniter\Validation\StrictRules\FileRules as StrictFileRules;
2117

2218
/**
2319
* File validation rules
2420
*
2521
* @see \CodeIgniter\Validation\FileRulesTest
2622
*/
27-
class FileRules
23+
class FileRules extends StrictFileRules
2824
{
29-
/**
30-
* Request instance. So we can get access to the files.
31-
*
32-
* @var IncomingRequest
33-
*/
34-
protected $request;
35-
36-
/**
37-
* Constructor.
38-
*/
39-
public function __construct(?RequestInterface $request = null)
40-
{
41-
if ($request === null) {
42-
$request = service('request');
43-
}
44-
45-
assert($request instanceof IncomingRequest || $request instanceof CLIRequest);
46-
47-
$this->request = $request;
48-
}
49-
50-
/**
51-
* Verifies that $name is the name of a valid uploaded file.
52-
*/
53-
public function uploaded(?string $blank, string $name): bool
54-
{
55-
if (! ($files = $this->request->getFileMultiple($name))) {
56-
$files = [$this->request->getFile($name)];
57-
}
58-
59-
foreach ($files as $file) {
60-
if ($file === null) {
61-
return false;
62-
}
63-
64-
if (ENVIRONMENT === 'testing') {
65-
if ($file->getError() !== 0) {
66-
return false;
67-
}
68-
} else {
69-
// Note: cannot unit test this; no way to over-ride ENVIRONMENT?
70-
// @codeCoverageIgnoreStart
71-
if (! $file->isValid()) {
72-
return false;
73-
}
74-
// @codeCoverageIgnoreEnd
75-
}
76-
}
77-
78-
return true;
79-
}
80-
81-
/**
82-
* Verifies if the file's size in Kilobytes is no larger than the parameter.
83-
*/
84-
public function max_size(?string $blank, string $params): bool
85-
{
86-
// Grab the file name off the top of the $params
87-
// after we split it.
88-
$paramArray = explode(',', $params);
89-
if (count($paramArray) !== 2) {
90-
throw new InvalidArgumentException('Invalid max_size parameter: "' . $params . '"');
91-
}
92-
$name = array_shift($paramArray);
93-
94-
if (! ($files = $this->request->getFileMultiple($name))) {
95-
$files = [$this->request->getFile($name)];
96-
}
97-
98-
foreach ($files as $file) {
99-
if ($file === null) {
100-
return false;
101-
}
102-
103-
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
104-
return true;
105-
}
106-
107-
if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
108-
return false;
109-
}
110-
111-
if ($file->getSize() / 1024 > $paramArray[0]) {
112-
return false;
113-
}
114-
}
115-
116-
return true;
117-
}
118-
119-
/**
120-
* Uses the mime config file to determine if a file is considered an "image",
121-
* which for our purposes basically means that it's a raster image or svg.
122-
*/
123-
public function is_image(?string $blank, string $params): bool
124-
{
125-
// Grab the file name off the top of the $params
126-
// after we split it.
127-
$params = explode(',', $params);
128-
$name = array_shift($params);
129-
130-
if (! ($files = $this->request->getFileMultiple($name))) {
131-
$files = [$this->request->getFile($name)];
132-
}
133-
134-
foreach ($files as $file) {
135-
if ($file === null) {
136-
return false;
137-
}
138-
139-
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
140-
return true;
141-
}
142-
143-
// We know that our mimes list always has the first mime
144-
// start with `image` even when then are multiple accepted types.
145-
$type = Mimes::guessTypeFromExtension($file->getExtension()) ?? '';
146-
147-
if (mb_strpos($type, 'image') !== 0) {
148-
return false;
149-
}
150-
}
151-
152-
return true;
153-
}
154-
155-
/**
156-
* Checks to see if an uploaded file's mime type matches one in the parameter.
157-
*/
158-
public function mime_in(?string $blank, string $params): bool
159-
{
160-
// Grab the file name off the top of the $params
161-
// after we split it.
162-
$params = explode(',', $params);
163-
$name = array_shift($params);
164-
165-
if (! ($files = $this->request->getFileMultiple($name))) {
166-
$files = [$this->request->getFile($name)];
167-
}
168-
169-
foreach ($files as $file) {
170-
if ($file === null) {
171-
return false;
172-
}
173-
174-
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
175-
return true;
176-
}
177-
178-
if (! in_array($file->getMimeType(), $params, true)) {
179-
return false;
180-
}
181-
}
182-
183-
return true;
184-
}
185-
186-
/**
187-
* Checks to see if an uploaded file's extension matches one in the parameter.
188-
*/
189-
public function ext_in(?string $blank, string $params): bool
190-
{
191-
// Grab the file name off the top of the $params
192-
// after we split it.
193-
$params = explode(',', $params);
194-
$name = array_shift($params);
195-
196-
if (! ($files = $this->request->getFileMultiple($name))) {
197-
$files = [$this->request->getFile($name)];
198-
}
199-
200-
foreach ($files as $file) {
201-
if ($file === null) {
202-
return false;
203-
}
204-
205-
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
206-
return true;
207-
}
208-
209-
if (! in_array($file->guessExtension(), $params, true)) {
210-
return false;
211-
}
212-
}
213-
214-
return true;
215-
}
216-
217-
/**
218-
* Checks an uploaded file to verify that the dimensions are within
219-
* a specified allowable dimension.
220-
*/
221-
public function max_dims(?string $blank, string $params): bool
222-
{
223-
// Grab the file name off the top of the $params
224-
// after we split it.
225-
$params = explode(',', $params);
226-
$name = array_shift($params);
227-
228-
if (! ($files = $this->request->getFileMultiple($name))) {
229-
$files = [$this->request->getFile($name)];
230-
}
231-
232-
foreach ($files as $file) {
233-
if ($file === null) {
234-
return false;
235-
}
236-
237-
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
238-
return true;
239-
}
240-
241-
// Get Parameter sizes
242-
$allowedWidth = $params[0] ?? 0;
243-
$allowedHeight = $params[1] ?? 0;
244-
245-
// Get uploaded image size
246-
$info = getimagesize($file->getTempName());
247-
248-
if ($info === false) {
249-
// Cannot get the image size.
250-
return false;
251-
}
252-
253-
$fileWidth = $info[0];
254-
$fileHeight = $info[1];
255-
256-
if ($fileWidth > $allowedWidth || $fileHeight > $allowedHeight) {
257-
return false;
258-
}
259-
}
260-
261-
return true;
262-
}
263-
264-
/**
265-
* Checks an uploaded file to verify that the dimensions are greater than
266-
* a specified dimension.
267-
*/
268-
public function min_dims(?string $blank, string $params): bool
269-
{
270-
// Grab the file name off the top of the $params
271-
// after we split it.
272-
$params = explode(',', $params);
273-
$name = array_shift($params);
274-
275-
$files = $this->request->getFileMultiple($name);
276-
if ($files === null) {
277-
$files = [$this->request->getFile($name)];
278-
}
279-
280-
foreach ($files as $file) {
281-
if ($file === null) {
282-
return false;
283-
}
284-
285-
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
286-
return true;
287-
}
288-
289-
// Get Parameter sizes
290-
$minimumWidth = $params[0] ?? 0;
291-
$minimumHeight = $params[1] ?? 0;
292-
293-
// Get uploaded image size
294-
$info = getimagesize($file->getTempName());
295-
296-
if ($info === false) {
297-
// Cannot get the image size.
298-
return false;
299-
}
300-
301-
$fileWidth = $info[0];
302-
$fileHeight = $info[1];
303-
304-
if ($fileWidth < $minimumWidth || $fileHeight < $minimumHeight) {
305-
return false;
306-
}
307-
}
308-
309-
return true;
310-
}
31125
}

0 commit comments

Comments
 (0)