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 12, 2023
1 parent b1233bb commit dc76ab9
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/mdphpcs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php

use PHP_CodeSniffer\Exceptions\DeepExitException;
use SilverStripe\MD_PHP_CodeSniffer\Sniffer;
use SilverStripe\MarkdownPhpCodeSniffer\Sniffer;

// php_codesniffer autoloader (which itself includes the composer autoloader)
$autoloadCandidates = [
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
},
"autoload": {
"psr-4": {
"SilverStripe\\MD_PHP_CodeSniffer\\": "src/",
"SilverStripe\\MD_PHP_CodeSniffer\\Tests\\": "tests/"
"SilverStripe\\MarkdownPhpCodeSniffer\\": "src/",
"SilverStripe\\MarkdownPhpCodeSniffer\\Tests\\": "tests/"
}
},
"bin": [
Expand Down
19 changes: 18 additions & 1 deletion src/CodeBlock.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace SilverStripe\MD_PHP_CodeSniffer;
namespace SilverStripe\MarkdownPhpCodeSniffer;

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
2 changes: 1 addition & 1 deletion src/FixerReport.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\MD_PHP_CodeSniffer;
namespace SilverStripe\MarkdownPhpCodeSniffer;

use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Files\File;
Expand Down
18 changes: 12 additions & 6 deletions src/Sniffer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\MD_PHP_CodeSniffer;
namespace SilverStripe\MarkdownPhpCodeSniffer;

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
Expand Down 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
8 changes: 4 additions & 4 deletions tests/CodeBlockTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace SilverStripe\MD_PHP_CodeSniffer\Test;
namespace SilverStripe\MarkdownPhpCodeSniffer\Test;

use ReflectionProperty;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHPUnit\Framework\TestCase;
use SilverStripe\MD_PHP_CodeSniffer\CodeBlock;
use SilverStripe\MarkdownPhpCodeSniffer\CodeBlock;

class CodeBlockTest extends TestCase
{
Expand All @@ -22,7 +22,7 @@ public static function setUpBeforeClass(): void
public function testGetContent()
{
$config = new Config();
$block = new CodeBlock('This is the content', new Ruleset($config), $config);
$block = new CodeBlock(new Ruleset($config), $config, 'This is the content', '', '', 0);

$this->assertSame('This is the content', $block->getContent());

Expand All @@ -34,7 +34,7 @@ public function testGetContent()
public function testCleanup()
{
$config = new Config();
$block = new CodeBlock('This is the content', new Ruleset($config), $config);
$block = new CodeBlock(new Ruleset($config), $config, 'This is the content', '', '', 0);
$block->cleanUp();

$this->assertSame('This is the content', $block->getContent());
Expand Down
4 changes: 2 additions & 2 deletions tests/SnifferFixTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace SilverStripe\MD_PHP_CodeSniffer\Test;
namespace SilverStripe\MarkdownPhpCodeSniffer\Test;

use PHP_CodeSniffer\Files\FileList;
use PHP_CodeSniffer\Ruleset;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use SilverStripe\MD_PHP_CodeSniffer\Sniffer;
use SilverStripe\MarkdownPhpCodeSniffer\Sniffer;

/**
* Because of PHP CodeSniffer's reliance on constants, this has to be done separately from the other tests.
Expand Down
4 changes: 2 additions & 2 deletions tests/SnifferTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace SilverStripe\MD_PHP_CodeSniffer\Test;
namespace SilverStripe\MarkdownPhpCodeSniffer\Test;

use PHP_CodeSniffer\Files\FileList;
use PHP_CodeSniffer\Ruleset;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use SilverStripe\MD_PHP_CodeSniffer\Sniffer;
use SilverStripe\MarkdownPhpCodeSniffer\Sniffer;

class SnifferTest extends TestCase
{
Expand Down

0 comments on commit dc76ab9

Please sign in to comment.