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

Commit 672614f

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

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
public $files = [
39+
'COPYRIGHT.md',
40+
'LICENSE.md',
41+
];
42+
43+
/**
44+
* @var bool
45+
*/
46+
private $run = false;
47+
48+
/**
49+
* @return int[]
50+
*/
51+
public function register() : array
52+
{
53+
return [
54+
T_INLINE_HTML,
55+
T_MD_LINE,
56+
T_OPEN_TAG,
57+
];
58+
}
59+
60+
/**
61+
* @param int $stackPtr
62+
* @return int
63+
*/
64+
public function process(File $phpcsFile, $stackPtr)
65+
{
66+
if ($this->run) {
67+
return $phpcsFile->numTokens + 1;
68+
}
69+
$this->run = true;
70+
71+
foreach ($this->files as $file) {
72+
if (! file_exists($file)) {
73+
$error = 'File %s does not exist';
74+
$data = [$file];
75+
$fix = $phpcsFile->addFixableError($error, 0, 'NotExists', $data);
76+
77+
if ($fix) {
78+
touch($file);
79+
}
80+
}
81+
}
82+
83+
return $phpcsFile->numTokens + 1;
84+
}
85+
}

0 commit comments

Comments
 (0)