Skip to content

Commit 5d09079

Browse files
committed
Run PHPCS setup code only once per instance of the XMLDocValidator class
1 parent 295f567 commit 5d09079

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

Scripts/DocCodeExamples/XmlDocValidator.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class XmlDocValidator
5959
*/
6060
private $phpcsConfig;
6161

62+
/**
63+
* The PHPCS ruleset object.
64+
*
65+
* @var \PHP_CodeSniffer\Ruleset
66+
*/
67+
private $phpcsRuleset;
68+
6269
/**
6370
* A helper object to write output.
6471
*
@@ -137,6 +144,8 @@ public function __construct(
137144
*/
138145
public function validate(): bool
139146
{
147+
$this->setUpPhpcs();
148+
140149
$validated = true;
141150

142151
$codeBlocks = $this->extractor->extract($this->path);
@@ -153,6 +162,29 @@ public function validate(): bool
153162
return $validated;
154163
}
155164

165+
/**
166+
* Set up the PHPCS to be able to run the sniff against a code example programmatically.
167+
*
168+
* @return void
169+
*/
170+
private function setUpPhpcs()
171+
{
172+
if (\defined('PHP_CODESNIFFER_VERBOSITY') === false) {
173+
\define('PHP_CODESNIFFER_VERBOSITY', 0);
174+
}
175+
176+
if (\defined('PHP_CODESNIFFER_CBF') === false) {
177+
\define('PHP_CODESNIFFER_CBF', false);
178+
}
179+
180+
$this->phpcsConfig->standards = [$this->standard];
181+
$this->phpcsConfig->sniffs = [$this->sniff];
182+
183+
new Tokens(); // @phpstan-ignore new.resultUnused
184+
185+
$this->phpcsRuleset = new Ruleset($this->phpcsConfig);
186+
}
187+
156188
/**
157189
* Verify code blocks of a given section (valid or invalid).
158190
*
@@ -164,7 +196,7 @@ public function validate(): bool
164196
*
165197
* @return bool
166198
*/
167-
protected function verifyCodeBlocks(array $codeBlocks, bool $isExpectedToBeValid): bool
199+
private function verifyCodeBlocks(array $codeBlocks, bool $isExpectedToBeValid): bool
168200
{
169201
$codeBlocksPassedValidation = true;
170202

@@ -208,27 +240,13 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
208240
$codeBlockBehavesAsExpected = false;
209241
}
210242

211-
if (\defined('PHP_CODESNIFFER_VERBOSITY') === false) {
212-
\define('PHP_CODESNIFFER_VERBOSITY', 0);
213-
}
214-
215-
if (\defined('PHP_CODESNIFFER_CBF') === false) {
216-
\define('PHP_CODESNIFFER_CBF', false);
217-
}
218-
219-
$this->phpcsConfig->standards = [$this->standard];
220-
$this->phpcsConfig->sniffs = [$this->sniff];
221-
222-
new Tokens(); // @phpstan-ignore new.resultUnused
223-
$ruleset = new Ruleset($this->phpcsConfig);
224-
225243
// Set a unique file name for the DummyFile instance to avoid cache conflicts for
226244
// sniffs that rely on PHPCSUtils functionality.
227245
$dummyFileName = md5($this->sniff . $codeBlock->position);
228246
$fileContents = 'phpcs_input_file: ' . $dummyFileName . PHP_EOL;
229247
$fileContents .= $codeBlockContent;
230248

231-
$file = new DummyFile($fileContents, $ruleset, $this->phpcsConfig);
249+
$file = new DummyFile($fileContents, $this->phpcsRuleset, $this->phpcsConfig);
232250
$file->process();
233251

234252
if ($isExpectedToBeValid === true && ($file->getErrorCount() !== 0 || $file->getWarningCount() !== 0)) {

0 commit comments

Comments
 (0)