Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMOPS-908: Updating compatibility with the BIC doc generator #25

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "magento/magento-semver",
"description": "Magento semantic version checker",
"version": "2.0.0",
"version": "3.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
13 changes: 13 additions & 0 deletions src/Analyzer/AbstractCodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,17 @@ protected function getFileName($context, $nodeName, $isBefore = true)
{
return $isBefore ? $this->fileBefore : $this->fileAfter;
}

/**
* Gets the AbstractCodeAnalyzers for content analysis (ex: method analyzer for classes)
*
* @param string $context
* @param string $fileBefore
* @param string $fileAfter
* @return AbstractCodeAnalyzer[]
*/
protected function getContentAnalyzers($context, $fileBefore, $fileAfter)
{
return [];
}
}
34 changes: 22 additions & 12 deletions src/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,7 @@ protected function reportChanged($report, $registryBefore, $registryAfter, $toVe
$classAfter = $afterNameMap[$key];

if ($classBefore !== $classAfter) {
/**
* @var AnalyzerInterface[] $analyzers
*/
$analyzers = [
new ClassMethodAnalyzer(static::CONTEXT, $fileBefore, $fileAfter, $this->dependencyGraph),
new PropertyAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassConstantAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassMethodExceptionAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassImplementsAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassExtendsAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassTraitAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
];
$analyzers = $this->getContentAnalyzers(static::CONTEXT, $fileBefore, $fileAfter);

foreach ($analyzers as $analyzer) {
$internalReport = $analyzer->analyze($classBefore, $classAfter);
Expand All @@ -129,6 +118,27 @@ protected function reportChanged($report, $registryBefore, $registryAfter, $toVe
}
}

/**
* Get the list of content analyzers
*
* @param string $context
* @param string $fileBefore
* @param string $fileAfter
* @return AbstractCodeAnalyzer[]
*/
protected function getContentAnalyzers($context, $fileBefore, $fileAfter)
{
return [
new ClassMethodAnalyzer($context, $fileBefore, $fileAfter, $this->dependencyGraph),
new PropertyAnalyzer($context, $fileBefore, $fileAfter),
new ClassConstantAnalyzer($context, $fileBefore, $fileAfter),
new ClassMethodExceptionAnalyzer($context, $fileBefore, $fileAfter),
new ClassImplementsAnalyzer($context, $fileBefore, $fileAfter),
new ClassExtendsAnalyzer($context, $fileBefore, $fileAfter),
new ClassTraitAnalyzer($context, $fileBefore, $fileAfter),
];
}

/**
* Get the class node registry
*
Expand Down
26 changes: 19 additions & 7 deletions src/Analyzer/InterfaceAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,7 @@ protected function reportChanged($report, $registryBefore, $registryAfter, $toVe
$interfaceAfter = $afterNameMap[$key];

if ($interfaceBefore !== $interfaceAfter) {
/** @var AnalyzerInterface[] $analyzers */
$analyzers = [
new ClassMethodAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassConstantAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new ClassMethodExceptionAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
new InterfaceExtendsAnalyzer(static::CONTEXT, $fileBefore, $fileAfter)
];
$analyzers = $this->getContentAnalyzers(static::CONTEXT, $fileBefore, $fileAfter);

foreach ($analyzers as $analyzer) {
$internalReport = $analyzer->analyze($interfaceBefore, $interfaceAfter);
Expand All @@ -133,4 +127,22 @@ protected function reportChanged($report, $registryBefore, $registryAfter, $toVe
}
}
}

/**
* Get the list of content analyzers
*
* @param string $context
* @param string $fileBefore
* @param string $fileAfter
* @return AbstractCodeAnalyzer[]
*/
protected function getContentAnalyzers($context, $fileBefore, $fileAfter)
{
return [
new ClassMethodAnalyzer($context, $fileBefore, $fileAfter),
new ClassConstantAnalyzer($context, $fileBefore, $fileAfter),
new ClassMethodExceptionAnalyzer($context, $fileBefore, $fileAfter),
new InterfaceExtendsAnalyzer($context, $fileBefore, $fileAfter)
];
}
}
19 changes: 14 additions & 5 deletions src/Analyzer/TraitAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ protected function reportChanged($report, $registryBefore, $registryAfter, $toVe
if ($traitBefore !== $traitAfter) {
$fileBefore = $this->getFileName($registryBefore, $traitName);
$fileAfter = $this->getFileName($registryAfter, $traitName);

/** @var AbstractCodeAnalyzer[] $analyzers */
$analyzers = [
new ClassMethodAnalyzer(static::CONTEXT, $fileBefore, $fileAfter),
];
$analyzers = $this->getContentAnalyzers(static::CONTEXT, $fileBefore, $fileAfter);

foreach ($analyzers as $analyzer) {
$internalReport = $analyzer->analyze($traitBefore, $traitAfter);
Expand All @@ -128,4 +124,17 @@ protected function reportChanged($report, $registryBefore, $registryAfter, $toVe
}
}
}

/**
* Get the list of content analyzers
*
* @param string $context
* @param string $fileBefore
* @param string $fileAfter
* @return AbstractCodeAnalyzer[]
*/
protected function getContentAnalyzers($context, $fileBefore, $fileAfter)
{
return [new ClassMethodAnalyzer($context, $fileBefore, $fileAfter)];
}
}
37 changes: 37 additions & 0 deletions src/MergedReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SemanticVersionChecker;

use PHPSemVerChecker\Report\Report;

class MergedReport extends Report
{
/**
* Merges with the given report including any non-standard contexts
*
* @param Report $report
* @return $this
*/
public function merge(Report $report)
{
foreach ($report->differences as $context => $levels) {
if (!key_exists($context, $this->differences)) {
$this->differences[$context] = $levels;
} else {
foreach ($levels as $level => $differences) {
$this->differences[$context][$level] = array_merge(
$this->differences[$context][$level],
$differences
);
}
}
}

return $this;
}
}
16 changes: 13 additions & 3 deletions src/ReportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class ReportBuilder

/**
* Define analyzer factory list for the different report types.
* @var array
* @var string[]
*/
private $analyzerList = [
private $analyzerFactoryClasses = [
ReportTypes::API => AnalyzerFactory::class,
ReportTypes::ALL => NonApiAnalyzerFactory::class,
ReportTypes::DB_SCHEMA => DbSchemaAnalyzerFactory::class,
Expand Down Expand Up @@ -118,6 +118,16 @@ protected function makeVersionReport()
return $report;
}

/**
* Get the mapping of report type -> analyzer factory
*
* @return string[]
*/
protected function getAnalyzerFactoryClasses()
{
return $this->analyzerFactoryClasses;
}

/**
* Create a report based on type
*
Expand Down Expand Up @@ -161,7 +171,7 @@ protected function buildReport()
/**
* @var AnalyzerFactoryInterface $factory
*/
foreach ($this->analyzerList as $reportType => $factory) {
foreach ($this->getAnalyzerFactoryClasses() as $reportType => $factory) {
/** @var AnalyzerInterface $analyzer */
$analyzer = (new $factory())->create($dependencyMap);
$tmpReport = $analyzer->analyze(
Expand Down
13 changes: 4 additions & 9 deletions src/Reporter/BreakingChangeTableReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,15 @@ public function __construct($apiChangeReport, $apiMembershipReport, $targetFile)
*/
public function output(OutputInterface $output)
{
$contexts = [
'class',
'function',
'interface',
'trait',
'database',
];
$reportContexts = array_keys($this->report->getDifferences());
$membershipContexts = array_keys($this->membershipReport->getDifferences());

foreach ($contexts as $context) {
foreach ($reportContexts as $context) {
$header = static::formatSectionHeader($this->targetFile, $context, 'breaking-change');
$this->outputChangeReport($output, $this->report, $context, $header);
}

foreach ($contexts as $context) {
foreach ($membershipContexts as $context) {
$header = static::formatSectionHeader($this->targetFile, $context, 'api-membership');
$this->outputChangeReport($output, $this->membershipReport, $context, $header);
}
Expand Down
96 changes: 96 additions & 0 deletions tests/Unit/MergedReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SemanticVersionChecker\Test\Unit;

use Magento\SemanticVersionChecker\InjectableReport;
use Magento\SemanticVersionChecker\MergedReport;
use Magento\SemanticVersionChecker\Operation\ClassConstantRemoved;
use Magento\SemanticVersionChecker\Operation\ClassConstructorOptionalParameterAdded;
use Magento\SemanticVersionChecker\Operation\ClassExtendsAdded;
use Magento\SemanticVersionChecker\Operation\ClassImplementsAdded;
use Magento\SemanticVersionChecker\Operation\ClassMethodOptionalParameterAdded;
use Magento\SemanticVersionChecker\Operation\ClassTraitAdded;
use Magento\SemanticVersionChecker\Operation\DropForeignKey;
use Magento\SemanticVersionChecker\Operation\SystemXml\FieldAdded;
use PHPSemVerChecker\Report\Report;
use PHPSemVerChecker\SemanticVersioning\Level;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MergedReportTest extends TestCase
{
/**
* Verify that reports are properly merged including non-standard contexts
*
* @return void
*/
public function testMergedChanges()
{
$diffsWithDatabase = [];
$diffsWithDatabase['class'][Level::PATCH] = [$this->createMock(ClassConstructorOptionalParameterAdded::class)];
$diffsWithDatabase['class'][Level::MINOR] = [
$this->createMock(ClassExtendsAdded::class),
$this->createMock(ClassMethodOptionalParameterAdded::class)
];
$diffsWithDatabase['database'][Level::MAJOR] = [$this->createMock(DropForeignKey::class)];
$databaseReport = new InjectableReport($diffsWithDatabase);

$diffsWithXml = [];
$diffsWithXml['class'][Level::MAJOR] = [$this->createMock(ClassConstantRemoved::class)];
$diffsWithXml['class'][Level::MINOR] = [$this->createMock(ClassImplementsAdded::class)];
$diffsWithXml['xml'][Level::MINOR] = [$this->createMock(FieldAdded::class)];
$xmlReport = new InjectableReport($diffsWithXml);

/** @var MockObject|ClassTraitAdded $preMergeOperation */
$preMergeOperation = $this->createMock(ClassTraitAdded::class);
$preMergeOperation->expects($this->any())->method('getLevel')->willReturn(Level::MINOR);

$mergedReport = new MergedReport();
$mergedReport->addClass($preMergeOperation);
$mergedReport->merge($databaseReport);
$mergedReport->merge($xmlReport);

$mergedDiffs = $mergedReport->getDifferences();
$this->assertTrue(key_exists('database', $mergedDiffs));
$this->assertTrue(key_exists('xml', $mergedDiffs));
$this->assertTrue($this->hasAllDifferences($mergedReport, $databaseReport));
$this->assertTrue($this->hasAllDifferences($mergedReport, $xmlReport));
$this->assertTrue(array_search($preMergeOperation, $mergedDiffs['class'][Level::MINOR]) !== false);
}

/**
* Checks if a merged report contains all differences in another report

* @param Report $mergedReport
* @param Report $inputReport
* @return bool
*/
private function hasAllDifferences($mergedReport, $inputReport)
{
$mergedDiffs = $mergedReport->getDifferences();
$inputDiffs = $inputReport->getDifferences();
foreach ($inputDiffs as $context => $levels) {
if (!key_exists($context, $mergedDiffs)) {
return false;
}
$mergedLevels = $mergedDiffs[$context];
foreach ($levels as $level => $operations) {
if (!key_exists($level, $mergedLevels)) {
return false;
}
$mergedOperations = $mergedLevels[$level];
foreach ($operations as $operation) {
if (array_search($operation, $mergedOperations) === false) {
return false;
}
}
}
}
return true;
}
}