Skip to content

Commit

Permalink
introduces AbstractNormalizer class and clean-up all normalizers acco…
Browse files Browse the repository at this point in the history
…rdingly
  • Loading branch information
llaville committed Jul 28, 2024
1 parent cbc5f68 commit e3374c8
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 226 deletions.
52 changes: 52 additions & 0 deletions src/Converter/Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/**
* This file is part of the Sarif-PHP-Converters package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\Sarif\Converter\Normalizer;

use Bartlett\Sarif\Contract\NormalizerInterface;

use ArrayObject;
use function in_array;

/**
* @author Laurent Laville
* @since Release 1.0.0
*/
abstract class AbstractNormalizer implements NormalizerInterface
{
public function getSupportedFormats(): array
{
return [
NormalizerInterface::FORMAT_INTERNAL,
];
}

public function normalize($data, string $format, array $context): ?ArrayObject
{
if (!in_array($format, $this->getSupportedFormats())) {
return null;
}

// internal format (legacy)
return new ArrayObject($this->fromInternal($data, $context));
}

/**
* @param mixed $data
* @param array<string, mixed> $context Options available to the normalizer
* @param array{}|array<string, string> $mapping From-To convert's mapping
* @return array{files: mixed, errors: mixed, rules: mixed}
*/
protected function fromInternal($data, array $context, array $mapping = []): array
{
return [
'files' => [],
'errors' => [],
'rules' => [],
];
}
}
46 changes: 1 addition & 45 deletions src/Converter/Normalizer/EcsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,10 @@
*/
namespace Bartlett\Sarif\Converter\Normalizer;

use Bartlett\Sarif\Contract\NormalizerInterface;

use ArrayObject;
use function in_array;

/**
* @author Laurent Laville
* @since Release 1.0.0
*/
final class EcsNormalizer implements NormalizerInterface
final class EcsNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
return [
NormalizerInterface::FORMAT_CHECKSTYLE,
NormalizerInterface::FORMAT_INTERNAL,
];
}

public function normalize($data, string $format, array $context): ?ArrayObject
{
if (!in_array($format, $this->getSupportedFormats())) {
return null;
}

$mapping = [

];

// internal format (legacy)
return new ArrayObject($this->fromInternal($data, $mapping));
}

/**
* @param mixed $data
* @param array<string, string> $mapping
* @return array{files: mixed, errors: mixed, rules: mixed}
*/
private function fromInternal($data, array $mapping): array
{
$files = [];
$errors = [];
$rules = [];

return [
'files' => $files,
'errors' => $errors,
'rules' => $rules,
];
}
}
46 changes: 1 addition & 45 deletions src/Converter/Normalizer/PhanNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,10 @@
*/
namespace Bartlett\Sarif\Converter\Normalizer;

use Bartlett\Sarif\Contract\NormalizerInterface;

use ArrayObject;
use function in_array;

/**
* @author Laurent Laville
* @since Release 1.0.0
*/
final class PhanNormalizer implements NormalizerInterface
final class PhanNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
return [
NormalizerInterface::FORMAT_CHECKSTYLE,
NormalizerInterface::FORMAT_INTERNAL,
];
}

public function normalize($data, string $format, array $context): ?ArrayObject
{
if (!in_array($format, $this->getSupportedFormats())) {
return null;
}

$mapping = [

];

// internal format (legacy)
return new ArrayObject($this->fromInternal($data, $mapping));
}

/**
* @param mixed $data
* @param array<string, string> $mapping
* @return array{files: mixed, errors: mixed, rules: mixed}
*/
private function fromInternal($data, array $mapping): array
{
$files = [];
$errors = [];
$rules = [];

return [
'files' => $files,
'errors' => $errors,
'rules' => $rules,
];
}
}
45 changes: 1 addition & 44 deletions src/Converter/Normalizer/PhpCsFixerNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,10 @@
*/
namespace Bartlett\Sarif\Converter\Normalizer;

use Bartlett\Sarif\Contract\NormalizerInterface;

use ArrayObject;
use function in_array;

/**
* @author Laurent Laville
* @since Release 1.0.0
*/
final class PhpCsFixerNormalizer implements NormalizerInterface
final class PhpCsFixerNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
return [
NormalizerInterface::FORMAT_INTERNAL,
];
}

public function normalize($data, string $format, array $context): ?ArrayObject
{
if (!in_array($format, $this->getSupportedFormats())) {
return null;
}

$mapping = [

];

// internal format (legacy)
return new ArrayObject($this->fromInternal($data, $mapping));
}

/**
* @param mixed $data
* @param array<string, string> $mapping
* @return array{files: mixed, errors: mixed, rules: mixed}
*/
private function fromInternal($data, array $mapping): array
{
$files = [];
$errors = [];
$rules = [];

return [
'files' => $files,
'errors' => $errors,
'rules' => $rules,
];
}
}
10 changes: 4 additions & 6 deletions src/Converter/Normalizer/PhpCsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Laurent Laville
* @since Release 1.0.0
*/
final class PhpCsNormalizer implements NormalizerInterface
final class PhpCsNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
Expand Down Expand Up @@ -50,15 +50,13 @@ public function normalize($data, string $format, array $context): ?ArrayObject
}

// internal format (legacy)
return new ArrayObject($this->fromInternal($data, $mapping));
return new ArrayObject($this->fromInternal($data, $context, $mapping));
}

/**
* @param array<string, array<int, mixed>|string> $data
* @param array<string, string> $mapping
* @return array{totals: array<string, int>, files: mixed, errors: mixed, rules: mixed}
* @inheritDoc
*/
private function fromInternal(array $data, array $mapping): array
protected function fromInternal($data, array $context, array $mapping = []): array
{
$filename = $data['filename'];
$errors = [];
Expand Down
20 changes: 4 additions & 16 deletions src/Converter/Normalizer/PhpLintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
namespace Bartlett\Sarif\Converter\Normalizer;

use Bartlett\Sarif\Contract\NormalizerInterface;

use ArrayObject;
use function array_keys;
use function in_array;
Expand All @@ -17,15 +15,8 @@
* @author Laurent Laville
* @since Release 1.0.0
*/
final class PhpLintNormalizer implements NormalizerInterface
final class PhpLintNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
return [
NormalizerInterface::FORMAT_INTERNAL,
];
}

public function normalize($data, string $format, array $context): ?ArrayObject
{
if (!in_array($format, $this->getSupportedFormats())) {
Expand All @@ -39,16 +30,13 @@ public function normalize($data, string $format, array $context): ?ArrayObject
];

// internal format (legacy)
return new ArrayObject($this->fromInternal($data, $mapping, $context));
return new ArrayObject($this->fromInternal($data, $context, $mapping));
}

/**
* @param array<string, mixed> $data
* @param array<string, string> $mapping
* @param array<string, mixed> $context Options available to the normalizer
* @return array{files: mixed, errors: mixed, rules: mixed}
* @inheritDoc
*/
private function fromInternal(array $data, array $mapping, array $context): array
protected function fromInternal($data, array $context, array $mapping = []): array
{
$ruleId = $context['rulePrefix'] . '101';

Expand Down
10 changes: 6 additions & 4 deletions src/Converter/Normalizer/PhpMdNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Laurent Laville
* @since Release 1.0.0
*/
final class PhpMdNormalizer implements NormalizerInterface
final class PhpMdNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
Expand Down Expand Up @@ -57,7 +57,7 @@ public function normalize($data, string $format, array $context): ?ArrayObject
}

// internal format (legacy)
return new ArrayObject($this->fromInternal($data));
return new ArrayObject($this->fromInternal($data, $context));
}

/**
Expand Down Expand Up @@ -134,14 +134,16 @@ private function fromCheckstyle(?ArrayObject $data): array
}

/**
* @return array{files: mixed, errors: mixed, rules: mixed}
* @inheritDoc
*/
private function fromInternal(Report $report): array
protected function fromInternal($data, array $context, array $mapping = []): array
{
$files = [];
$errors = [];
$rules = [];

$report = $data;

/** @var RuleViolation[] $violations */
$violations = $report->getRuleViolations();

Expand Down
18 changes: 5 additions & 13 deletions src/Converter/Normalizer/PhpStanNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
namespace Bartlett\Sarif\Converter\Normalizer;

use Bartlett\Sarif\Contract\NormalizerInterface;

use PHPStan\Command\AnalysisResult;

use ArrayObject;
Expand All @@ -19,15 +17,8 @@
* @author Laurent Laville
* @since Release 1.0.0
*/
final class PhpStanNormalizer implements NormalizerInterface
final class PhpStanNormalizer extends AbstractNormalizer
{
public function getSupportedFormats(): array
{
return [
NormalizerInterface::FORMAT_INTERNAL,
];
}

public function normalize($data, string $format, array $context): ?ArrayObject
{
if (!in_array($format, $this->getSupportedFormats())) {
Expand All @@ -43,14 +34,15 @@ public function normalize($data, string $format, array $context): ?ArrayObject
}

/**
* @param array<string, mixed> $context Options available to the normalizer
* @return array{files: mixed, errors: mixed, rules: mixed}
* @inheritDoc
*/
private function fromInternal(AnalysisResult $analysisResult, array $context): array
protected function fromInternal($data, array $context, array $mapping = []): array
{
$files = [];
$errors = [];

$analysisResult = $data;

$ruleId = $context['rulePrefix'] . '101';

foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
Expand Down
Loading

0 comments on commit e3374c8

Please sign in to comment.