Skip to content

Commit 3cf2a04

Browse files
committed
add downgrade support
1 parent 104ae69 commit 3cf2a04

File tree

7 files changed

+35
-10
lines changed

7 files changed

+35
-10
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Formatter;
4+
5+
use Composer\DependencyResolver\Operation\UpdateOperation;
6+
use Composer\Semver\Semver;
7+
use Composer\Semver\VersionParser;
8+
9+
abstract class AbstractFormatter implements Formatter
10+
{
11+
protected static function isUpgrade(UpdateOperation $operation)
12+
{
13+
$versionParser = new VersionParser();
14+
$normalizedFrom = $versionParser->normalize($operation->getInitialPackage()->getFullPrettyVersion());
15+
$normalizedTo = $versionParser->normalize($operation->getTargetPackage()->getFullPrettyVersion());
16+
17+
$sorted = Semver::sort(array($normalizedTo, $normalizedFrom));
18+
19+
return $sorted[0] === $normalizedFrom;
20+
}
21+
}

src/Formatter/MarkdownListFormatter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Composer\DependencyResolver\Operation\UpdateOperation;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

11-
class MarkdownListFormatter implements Formatter
11+
class MarkdownListFormatter extends AbstractFormatter
1212
{
1313
/**
1414
* @var OutputInterface
@@ -55,7 +55,8 @@ private function getRow(OperationInterface $operation)
5555

5656
if ($operation instanceof UpdateOperation) {
5757
return sprintf(
58-
' - Update %s (%s => %s)',
58+
' - %s %s (%s => %s)',
59+
self::isUpgrade($operation) ? 'Upgrade' : 'Downgrade',
5960
$operation->getInitialPackage()->getName(),
6061
$operation->getInitialPackage()->getFullPrettyVersion(),
6162
$operation->getTargetPackage()->getFullPrettyVersion()
@@ -64,7 +65,7 @@ private function getRow(OperationInterface $operation)
6465

6566
if ($operation instanceof UninstallOperation) {
6667
return sprintf(
67-
' - Remove %s (%s)',
68+
' - Uninstall %s (%s)',
6869
$operation->getPackage()->getName(),
6970
$operation->getPackage()->getFullPrettyVersion()
7071
);

src/Formatter/MarkdownTableFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use IonBazan\ComposerDiff\Formatter\Helper\MarkdownTable;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111

12-
class MarkdownTableFormatter implements Formatter
12+
class MarkdownTableFormatter extends AbstractFormatter
1313
{
1414
/**
1515
* @var OutputInterface

tests/Command/DiffCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function outputDataProvider()
5858
=============
5959
6060
- Install a/package-1 (1.0.0)
61-
- Update a/package-2 (1.0.0 => 1.2.0)
62-
- Remove a/package-3 (0.1.1)
61+
- Upgrade a/package-2 (1.0.0 => 1.2.0)
62+
- Uninstall a/package-3 (0.1.1)
6363
6464
6565
OUTPUT

tests/Formatter/FormatterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function testItRendersTheListOfOperations()
2727
$formatter->render(array(
2828
new InstallOperation($this->getPackage('a/package-1', '1.0.0')),
2929
new UpdateOperation($this->getPackage('a/package-2', '1.0.0'), $this->getPackage('a/package-2', '1.2.0')),
30-
new UninstallOperation($this->getPackage('a/package-3', '0.1.1')),
30+
new UpdateOperation($this->getPackage('a/package-3', '2.0.0'), $this->getPackage('a/package-3', '1.1.1')),
31+
new UninstallOperation($this->getPackage('a/package-4', '0.1.1')),
3132
), 'Test');
3233
$this->assertSame($this->getSampleOutput(), $this->getDisplay($output));
3334
}

tests/Formatter/MarkdownListFormatterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ protected function getSampleOutput()
1414
====
1515
1616
- Install a/package-1 (1.0.0)
17-
- Update a/package-2 (1.0.0 => 1.2.0)
18-
- Remove a/package-3 (0.1.1)
17+
- Upgrade a/package-2 (1.0.0 => 1.2.0)
18+
- Downgrade a/package-3 (2.0.0 => 1.1.1)
19+
- Uninstall a/package-4 (0.1.1)
1920
2021
2122
OUTPUT;

tests/Formatter/MarkdownTableFormatterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ protected function getSampleOutput()
1414
|-------------|-------|---------|
1515
| a/package-1 | New | 1.0.0 |
1616
| a/package-2 | 1.0.0 | 1.2.0 |
17-
| a/package-3 | 0.1.1 | Removed |
17+
| a/package-3 | 2.0.0 | 1.1.1 |
18+
| a/package-4 | 0.1.1 | Removed |
1819
1920
2021
OUTPUT;

0 commit comments

Comments
 (0)