Skip to content

Commit

Permalink
Fix compatibility issues and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed Nov 7, 2024
1 parent 6aca3b0 commit 844bd11
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
44 changes: 9 additions & 35 deletions src/Url/WordPressGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,42 @@
class WordPressGenerator implements UrlGenerator
{
/**
* Determines if the generator supports the given package.
*
* @return bool
* {@inheritdoc}
*/
public function supportsPackage(PackageInterface $package)
{
return 0 === strpos($package->getName(), 'wpackagist-plugin/') || 0 === strpos($package->getName(), 'wpackagist-theme/');
return (bool) preg_match('#^wpackagist-(plugin|theme)/#', $package->getName());
}

/**
* Generates a compare URL for two versions of the same package.
*
* @return string|null
* {@inheritdoc}
*/
public function getCompareUrl(PackageInterface $initialPackage, PackageInterface $targetPackage)
{
return null;
}

/**
* Generates URL for viewing a release or commit of a package.
*
* @return string|null
* {@inheritdoc}
*/
public function getReleaseUrl(PackageInterface $package)
{
return null;
}

/**
* Generates URL for viewing the project page of a package (usually repository root).
*
* @return string|null
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
$type = $this->getPackageType($package);
preg_match('#wpackagist-(plugin|theme)/(.+)#', $package->getName(), $matches);

if (null === $type) {
if (empty($matches)) {
return null;

Check warning on line 41 in src/Url/WordPressGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/Url/WordPressGenerator.php#L41

Added line #L41 was not covered by tests
}

return sprintf('https://wordpress.org/%ss/%s', $type, $this->getPackageSlug($package));
}

/**
* @return string|null
*/
protected function getPackageType(PackageInterface $package)
{
[$type] = explode('/', $package->getName(), 2);

return 0 === strpos($type, 'wpackagist-') ? substr($type, 11) : null;
}

/**
* @return string
*/
protected function getPackageSlug(PackageInterface $package)
{
[, $slug] = explode('/', $package->getName(), 2);
list (, $type, $slug) = $matches;

return $slug;
return sprintf('https://wordpress.org/%ss/%s', $type, $slug);
}
}
10 changes: 10 additions & 0 deletions tests/Url/WordPressGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class WordPressGeneratorTest extends GeneratorTest
{
public function testItSupportsOnlyWpackagistPackages()
{
$generator = $this->getGenerator();

$this->assertFalse($generator->supportsPackage($this->getPackage('acme/package', '3.12.1')));
$this->assertTrue($generator->supportsPackage($this->getPackage('wpackagist-plugin/my-plugin', '3.12.1')));
$this->assertTrue($generator->supportsPackage($this->getPackage('wpackagist-theme/my-theme', '3.12.1')));
$this->assertFalse($generator->supportsPackage($this->getPackage('acme-wpackagist-theme/my-theme', '3.12.1')));
}

public function releaseUrlProvider()
{
return array(
Expand Down

0 comments on commit 844bd11

Please sign in to comment.