Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit fe7a622

Browse files
committed
Added Files/Create sniff to create required files if these do not exist
1 parent 6a903cb commit fe7a622

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-coding-standard for the canonical source repository
4+
* @copyright https://github.com/zendframework/zend-coding-standard/blob/master/COPYRIGHT.md Copyright
5+
* @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace ZendCodingStandard\Sniffs\Files;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
15+
use function file_exists;
16+
use function touch;
17+
18+
use const T_INLINE_HTML;
19+
use const T_MD_LINE;
20+
use const T_OPEN_TAG;
21+
22+
/**
23+
* This sniff should run only once to create all missing files.
24+
* It's hacky solution, because sniffs works only on existing
25+
* files. So here we try to run the sniff on any file to check
26+
* for required files, and on fixing we create them.
27+
*/
28+
class CreateSniff implements Sniff
29+
{
30+
/**
31+
* @var string[]
32+
*/
33+
public $supportedTokenizers = [
34+
'MD',
35+
'PHP',
36+
];
37+
38+
/**
39+
* @var string[]
40+
*/
41+
public $files = [
42+
'COPYRIGHT.md',
43+
'LICENSE.md',
44+
];
45+
46+
/**
47+
* @var bool
48+
*/
49+
private $run = false;
50+
51+
/**
52+
* @return int[]
53+
*/
54+
public function register() : array
55+
{
56+
return [
57+
T_INLINE_HTML,
58+
T_MD_LINE,
59+
T_OPEN_TAG,
60+
];
61+
}
62+
63+
/**
64+
* @param int $stackPtr
65+
* @return int
66+
*/
67+
public function process(File $phpcsFile, $stackPtr)
68+
{
69+
if ($this->run) {
70+
return $phpcsFile->numTokens + 1;
71+
}
72+
$this->run = true;
73+
74+
foreach ($this->files as $file) {
75+
if (! file_exists($file)) {
76+
$error = 'File %s does not exist';
77+
$data = [$file];
78+
$fix = $phpcsFile->addFixableError($error, 0, 'NotExists', $data);
79+
80+
if ($fix) {
81+
touch($file);
82+
}
83+
}
84+
}
85+
86+
return $phpcsFile->numTokens + 1;
87+
}
88+
}

0 commit comments

Comments
 (0)