public function testAutoFix()
 {
     $code = '<?php $my__var_name_ = 1;';
     $linter = new PsrLinter([DefaultRules::class]);
     $linter->lint($code, true);
     $this->assertEquals('$MyVarName = 1;', $linter->getPrettyCode());
 }
示例#2
0
 /**
  * @param $params
  * @return int
  */
 public function run($params)
 {
     $srcPath = $params[0];
     $rulesPath = $params[1];
     $autoFix = $params['fix'];
     $makeReport = $params['report'];
     $reportFormat = $params['format'];
     $reportFileName = $params['output'];
     $rulesLoader = new RulesLoader();
     $rules = $rulesLoader->loadRules($rulesPath);
     $this->cli->lightGreen("Loading rules:");
     $this->printLog($rulesLoader->getLog());
     $this->cli->out('');
     if ($rules == []) {
         $this->printErrorMsg('No rules found');
         return 1;
     }
     $linter = new Linter\PsrLinter($rules);
     $reportBuilder = new ReportBuilder\ReportBulder($reportFormat);
     $exitVal = 0;
     $targetFiles = getTargetFiles($srcPath);
     foreach ($targetFiles as $target) {
         if (!file_exists($target)) {
             $this->printErrorMsg("File not found: {$target}");
             $exitVal = 1;
             continue;
         }
         $code = file_get_contents($target);
         $logger = $linter->lint($code, $autoFix);
         if ($autoFix) {
             $prettyCode = $linter->getPrettyCode();
             file_put_contents($target, '<?php' . PHP_EOL . $prettyCode);
         }
         if ($logger->getSize() != 0) {
             $this->cli->lightGreen("{$target}");
             $this->cli->br();
             $this->printLog($logger);
             $this->cli->br();
             $this->printLogStat($logger);
             $this->cli->br();
             $exitVal = 1;
         }
         if ($makeReport === true && $logger->getSize() != 0) {
             $reportBuilder->addSection($target, $logger);
         }
     }
     if ($makeReport === true) {
         file_put_contents($reportFileName, $reportBuilder->build());
     }
     return $exitVal;
 }