Skip to content

Commit c6c3fe5

Browse files
Merge pull request #26 from VincentLanglet/staging
✨ Add check for filename
2 parents 48f96cc + f2020a7 commit c6c3fe5

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Sniffs\NamingConventions;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Checks whether filename contains any other character than alphanumeric and underscores.
10+
*/
11+
class ValidFileNameSniff implements Sniff
12+
{
13+
/**
14+
* Returns an array of tokens this test wants to listen for.
15+
*
16+
* @return array
17+
*/
18+
public function register()
19+
{
20+
return array(T_OPEN_TAG);
21+
}
22+
23+
/**
24+
* Process.
25+
*
26+
* @param File $phpcsFile
27+
* @param int $stackPtr
28+
*
29+
* @return void
30+
*/
31+
public function process(File $phpcsFile, $stackPtr)
32+
{
33+
$filename = $phpcsFile->getFilename();
34+
35+
if ('STDIN' === $filename) {
36+
return;
37+
}
38+
39+
$filenamePhp = str_replace('_', '', basename($filename, '.php'));
40+
$filenameInc = str_replace('_', '', basename($filename, '.inc'));
41+
42+
if (strlen($filenameInc) < strlen($filenamePhp)) {
43+
$filename = $filenameInc;
44+
} else {
45+
$filename = $filenamePhp;
46+
}
47+
48+
if (false === ctype_alnum($filename)) {
49+
$error = sprintf('Filename "%s" contains non alphanumeric characters', $filename);
50+
$phpcsFile->addError($error, $stackPtr, 'Invalid');
51+
$phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'no');
52+
} else {
53+
$phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'yes');
54+
}
55+
56+
return;
57+
}
58+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return 'Test';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return 'Test';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Tests\NamingConventions;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the ValidFileName sniff.
9+
*
10+
* @group Symfony3Custom
11+
*/
12+
class ValidFileNameUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @param string $filename
21+
*
22+
* @return array<int, int>
23+
*/
24+
public function getErrorList($filename = '')
25+
{
26+
switch ($filename) {
27+
case 'ValidFileNameUnitTest.inc':
28+
return [];
29+
case 'ValidFileNameUnitTest.Invalid.inc':
30+
return [
31+
1 => 1,
32+
];
33+
default:
34+
return [];
35+
}
36+
}
37+
38+
/**
39+
* Returns the lines where warnings should occur.
40+
*
41+
* The key of the array should represent the line number and the value
42+
* should represent the number of warnings that should occur on that line.
43+
*
44+
* @return array(int => int)
45+
*/
46+
protected function getWarningList()
47+
{
48+
return array();
49+
}
50+
}

docs/standards/symfony.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ Covered by `PSR1` completed by
159159
<rule ref="Symfony3Custom.NamingConventions.ValidClassName" />
160160
```
161161

162+
- Use alphanumeric characters and underscores for file names
163+
164+
```
165+
<rule ref="Symfony3Custom.NamingConventions.ValidFileName" />
166+
```
167+
162168
- For type-hinting in PHPDocs and casting, use `bool`, `int` and `float`
163169

164170
```

0 commit comments

Comments
 (0)