Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Simplified constructor for LinkedVersions collection and also simplif…
Browse files Browse the repository at this point in the history
…ied isAcceptable()
  • Loading branch information
gsomoza committed Jul 13, 2015
1 parent dc05241 commit 243e1f4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
11 changes: 3 additions & 8 deletions lib/Version/Collection/IndexedVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,13 @@ public function __construct($versions = array())
* Returns true if the specified version is valid (can be added) to the collection. Otherwise, it MUST throw
* an exception.
*
* @param mixed $version
* @param Version $version
* @return bool
* @throws InvalidArgumentException
*/
public function isAcceptable($version)
public function isAcceptable(Version $version)
{
if (!is_object($version) || !$version instanceof Version) {
throw new InvalidArgumentException(
sprintf('Expected version to be of type "%s"', Version::class)
);
}
return true;
return $version; // always true
}

/**
Expand Down
9 changes: 4 additions & 5 deletions lib/Version/Collection/LinkedVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ class LinkedVersions extends SortableVersions
/**
* Validates that migrations added to this set must all have a linked Migration
*
* @param mixed $version
* @param Version $version
* @return bool
*
* @throws MigrationMissingException
*/
public function isAcceptable($version)
public function isAcceptable(Version $version)
{
/** @var Version $version */
if (parent::isAcceptable($version) && !$version->hasMigration()) {
if (!$version->hasMigration()) {
throw new MigrationMissingException(sprintf(
'Version "%s" must be associated with a Migration in order to be accepted into this collection.',
$version->getId()
));
}
return true;
return parent::isAcceptable($version);
}
}
9 changes: 4 additions & 5 deletions lib/Version/Collection/MigratedVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ class MigratedVersions extends SortableVersions
/**
* This makes the collection behave like a set - throwing an exception if the version already exists in the set.
*
* @param mixed $version
* @param Version $version
* @return bool
* @throws CollectionException
* @throws MigrationMissingException
*/
public function isAcceptable($version)
public function isAcceptable(Version $version)
{
/** @var Version $version */
if (parent::isAcceptable($version) && !$version->isMigrated()) {
if (!$version->isMigrated()) {
throw new CollectionException(sprintf(
'Version "%s" must be migrated in order to be accepted into this collection.',
$version->getId()
));
}
return true;
return parent::isAcceptable($version);
}
}
9 changes: 4 additions & 5 deletions lib/Version/Collection/SortableVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ class SortableVersions extends IndexedVersions
/**
* This makes the collection behave like a set - throwing an exception if the version already exists in the set.
*
* @param mixed $version
* @param Version $version
* @return bool
*
* @throws CollectionException
*/
public function isAcceptable($version)
public function isAcceptable(Version $version)
{
/** @var Version $version */
if (parent::isAcceptable($version) && $this->has($version->getId())) {
if ($this->has($version->getId())) {
throw new CollectionException(
sprintf('Item with id "%s" already exists', $version->getId())
);
}
return true;
return parent::isAcceptable($version);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions test/Version/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ public function testConstructorIterator()
$this->assertCount(3, $instance);
}

public function testConstructorWithInvalidVersions()
{
$versions = [new V('1'), '2', 3, new \stdClass()]; // only first one is valid

$this->setExpectedException(\Exception::class);
$instance = new SortableVersions($versions);
}

public function testConstructor()
{
$instance = new SortableVersions();
Expand Down

0 comments on commit 243e1f4

Please sign in to comment.