Skip to content

Commit

Permalink
ENH Tidy-up code
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 1, 2023
1 parent b1233bb commit 1e5c514
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/CodeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace SilverStripe\MD_PHP_CodeSniffer;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Ruleset;

class CodeBlock extends DummyFile
{
Expand All @@ -12,6 +14,21 @@ class CodeBlock extends DummyFile

private string $finalContent = '';

public function __construct(
Ruleset $ruleset,
Config $config,
string $content,
string $path,
string $realPath,
int $num
) {
parent::__construct($content, $ruleset, $config);

$this->path = $path;
$this->realPath = $realPath;
$this->num = $num;
}

public function cleanUp()
{
$this->finalContent = $this->content ?? '';
Expand Down
16 changes: 11 additions & 5 deletions src/Sniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ public function run(string $lintLanguage, bool $fixing, bool $usingExplicitStand
// Add code blocks to the file list for linting
$todo = [];
foreach ($codeBlocks as $block) {
$dummy = new CodeBlock($block['content'], $sniffer->ruleset, $sniffer->config);
$dummy->num = $block['num'];
$dummy->path = $block['path'];
$dummy->realPath = $block['realpath'];
$dummy = new CodeBlock(
$sniffer->ruleset,
$sniffer->config,
$block['content'],
$block['path'],
$block['realpath'],
$block['num']
);
$todo[] = $dummy;
}

Expand Down Expand Up @@ -107,6 +111,7 @@ public function run(string $lintLanguage, bool $fixing, bool $usingExplicitStand

$sniffer->reporter->printReports();

// These return values are directly from Runner::runPHPCS()
if ($numErrors === 0) {
// No errors found.
return 0;
Expand Down Expand Up @@ -148,6 +153,7 @@ private function prepareConfig(bool $usingExplicitStandard, string $lintLanguage
$config->standards = [__DIR__ . '/../phpcs.default.xml'];
}

// Most of these overrides are directly from Runner::runPHPCBF()
if ($fixing) {
// Override some of the command line settings that might break the fixes.
$config->generator = null;
Expand Down Expand Up @@ -228,7 +234,7 @@ private function sniff(Runner $sniffer, array $todo): int
// Turn all sniff errors into exceptions.
set_error_handler([$sniffer, 'handleErrors']);

$lastDir = '';
$lastDir = '';
$numBlocks = count($todo);

// Process each block sequentially - running sniff in parallel isn't supported
Expand Down

0 comments on commit 1e5c514

Please sign in to comment.