Skip to content

Commit 8ea12cb

Browse files
author
dereuromark
committed
Fix tokenizer.
1 parent becaa0c commit 8ea12cb

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

PSR2R/Tools/Tokenizer.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
namespace PSR2R\Tools;
44

55
use Exception;
6-
use PHP_CodeSniffer;
6+
use PHP_CodeSniffer\Config;
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Reporter;
9+
use PHP_CodeSniffer\Ruleset;
10+
use PHP_CodeSniffer\Runner;
11+
12+
$manualAutoload = getcwd() . '/vendor/squizlabs/php_codesniffer/autoload.php';
13+
if (!class_exists(Config::class) && file_exists($manualAutoload)) {
14+
require $manualAutoload;
15+
}
716

817
class Tokenizer {
918

@@ -54,6 +63,7 @@ public function tokenize() {
5463
$_SERVER['argc'] = count($_SERVER['argv']);
5564
$res = [];
5665
$tokens = $this->_getTokens($this->path);
66+
5767
$array = file($this->path);
5868
foreach ($array as $key => $row) {
5969
$res[] = rtrim($row);
@@ -76,10 +86,20 @@ public function tokenize() {
7686
* @return array Tokens
7787
*/
7888
protected function _getTokens($path) {
79-
$phpcs = new PHP_CodeSniffer();
80-
$phpcs->process([], $this->root . static::STANDARD, []);
81-
$file = $phpcs->processFile($path);
82-
$file->start();
89+
$phpcs = new Runner();
90+
91+
define('PHP_CODESNIFFER_CBF', false);
92+
93+
$config = new Config(['--standard=PSR2']);
94+
$phpcs->config = $config;
95+
$phpcs->init();
96+
97+
$ruleset = new Ruleset($config);
98+
99+
$file = new File($path, $ruleset, $config);
100+
$file->setContent(file_get_contents($path));
101+
$file->parse();
102+
83103
return $file->getTokens();
84104
}
85105

tests/PSR2R/Base/AbstractBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class AbstractBase extends AbstractSniffUnitTest {
1717
protected function setUp() {
1818
parent::setUp();
1919
$config = new Config();
20-
/* @noinspection PhpUndefinedFieldInspection */
20+
/** @noinspection PhpUndefinedFieldInspection */
2121
$config->cache = false;
2222
$ruleset = new Ruleset($config);
2323
$GLOBALS['PHP_CODESNIFFER_RULESET'] = $ruleset;

0 commit comments

Comments
 (0)