Skip to content

Commit

Permalink
Refactor code to avoid extending the sebastian/comparator factory
Browse files Browse the repository at this point in the history
Version 5 of the library makes the Factory class final.
  • Loading branch information
stof committed Feb 1, 2023
1 parent 2937f44 commit 85417e3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@

"autoload-dev": {
"psr-4": {
"Fixtures\\Prophecy\\": "fixtures"
"Fixtures\\Prophecy\\": "fixtures",
"Tests\\Prophecy\\": "tests"
}
},

Expand Down
5 changes: 3 additions & 2 deletions src/Prophecy/Argument/Token/ExactValueToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace Prophecy\Argument\Token;

use Prophecy\Comparator\FactoryProvider;
use SebastianBergmann\Comparator\ComparisonFailure;
use Prophecy\Comparator\Factory as ComparatorFactory;
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
use Prophecy\Util\StringUtil;

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct($value, StringUtil $util = null, ComparatorFactory $
$this->value = $value;
$this->util = $util ?: new StringUtil();

$this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
$this->comparatorFactory = $comparatorFactory ?: FactoryProvider::getInstance();
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/Prophecy/Argument/Token/ObjectStateToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace Prophecy\Argument\Token;

use Prophecy\Comparator\FactoryProvider;
use SebastianBergmann\Comparator\ComparisonFailure;
use Prophecy\Comparator\Factory as ComparatorFactory;
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
use Prophecy\Util\StringUtil;

/**
Expand All @@ -30,10 +31,8 @@ class ObjectStateToken implements TokenInterface
/**
* Initializes token.
*
* @param string $methodName
* @param mixed $value Expected return value
* @param null|StringUtil $util
* @param ComparatorFactory $comparatorFactory
* @param string $methodName
* @param mixed $value Expected return value
*/
public function __construct(
$methodName,
Expand All @@ -45,7 +44,7 @@ public function __construct(
$this->value = $value;
$this->util = $util ?: new StringUtil;

$this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
$this->comparatorFactory = $comparatorFactory ?: FactoryProvider::getInstance();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Prophecy/Comparator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Prophecy comparator factory.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*
* @deprecated Use "Prophecy\Comparator\FactoryProvider" instead to get a "SebastianBergmann\Comparator\Factory" instance.
*/
final class Factory extends BaseFactory
{
Expand Down
42 changes: 42 additions & 0 deletions src/Prophecy/Comparator/FactoryProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Prophecy\Comparator;

use SebastianBergmann\Comparator\Factory;

/**
* Prophecy comparator factory.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
final class FactoryProvider
{
/**
* @var Factory|null
*/
private static $instance;

private function __construct()
{
}

public static function getInstance(): Factory
{
if (self::$instance === null) {
self::$instance = new Factory();
self::$instance->register(new ClosureComparator());
self::$instance->register(new ProphecyComparator());
}

return self::$instance;
}
}
5 changes: 3 additions & 2 deletions src/Prophecy/Prophecy/ObjectProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace Prophecy\Prophecy;

use Prophecy\Comparator\FactoryProvider;
use SebastianBergmann\Comparator\ComparisonFailure;
use Prophecy\Comparator\Factory as ComparatorFactory;
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
use Prophecy\Call\Call;
use Prophecy\Doubler\LazyDouble;
use Prophecy\Argument\ArgumentsWildcard;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function __construct(
$this->callCenter = $callCenter ?: new CallCenter;
$this->revealer = $revealer ?: new Revealer;

$this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
$this->comparatorFactory = $comparatorFactory ?: FactoryProvider::getInstance();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Comparator/FactoryProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Prophecy\Comparator;

use Prophecy\Comparator\ClosureComparator;
use Prophecy\Comparator\FactoryProvider;
use PHPUnit\Framework\TestCase;

class FactoryProviderTest extends TestCase
{
/**
* @test
*/
function it_should_have_ClosureComparator_registered()
{
$comparator = FactoryProvider::getInstance()->getComparatorFor(function(){}, function(){});

$this->assertInstanceOf(ClosureComparator::class, $comparator);
}
}

0 comments on commit 85417e3

Please sign in to comment.