From b0d3270dc85e0f1962d9382b715f154aa673b8ab Mon Sep 17 00:00:00 2001 From: Dimitri Gritsajuk Date: Thu, 24 Jun 2021 11:51:02 +0200 Subject: [PATCH 1/2] Update package to php7 --- .gitignore | 1 + .travis.yml | 10 +-- composer.json | 8 +-- src/AbstractSitemap.php | 67 +++++++------------ src/ChangeFrequency.php | 14 ++-- .../MaxUrlCountExceededException.php | 7 ++ src/Sitemap.php | 11 +-- src/SitemapFactory.php | 63 +++++++---------- src/SitemapIndex.php | 9 +-- src/functions.php | 13 ++-- tests/AbstractSitemapTest.php | 13 ++-- tests/FunctionsTest.php | 11 +-- tests/SitemapFactoryTest.php | 14 ++-- tests/SitemapIndexTest.php | 4 +- tests/SitemapTest.php | 4 +- 15 files changed, 111 insertions(+), 138 deletions(-) create mode 100644 src/Exception/MaxUrlCountExceededException.php diff --git a/.gitignore b/.gitignore index 5e8a691..db495c4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .DS_Store composer.lock +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index 23d335b..afc2a9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ language: php php: - - 5.4 - - 5.5 - - 5.6 - - hhvm + - 7.3 + - 7.4 + - 8.0 before_script: - composer self-update @@ -12,7 +11,4 @@ before_script: script: phpunit matrix: - allow_failures: - - php: 5.6 - - php: hhvm fast_finish: true diff --git a/composer.json b/composer.json index 551fa04..31d0310 100644 --- a/composer.json +++ b/composer.json @@ -9,13 +9,13 @@ "email": "dh@tackk.com" } ], - "minimum-stability": "dev", "require-dev": { - "phpunit/phpunit": "4.0.*" + "phpunit/phpunit": "^9.5" }, "require": { - "php": ">=5.4", - "league/flysystem": "1.0.*" + "php": ">=7.3", + "league/flysystem": "^1.0.0", + "ext-dom": "*" }, "autoload": { "files": [ diff --git a/src/AbstractSitemap.php b/src/AbstractSitemap.php index e8964c2..0ca6df1 100644 --- a/src/AbstractSitemap.php +++ b/src/AbstractSitemap.php @@ -2,33 +2,21 @@ namespace Tackk\Cartographer; -use DateTime; -use DateTimeZone; -use DOMDocument; -use DOMElement; -use InvalidArgumentException; -use RuntimeException; - -class MaxUrlCountExceededException extends RuntimeException -{ -} - +use Tackk\Cartographer\Exception\MaxUrlCountExceededException; abstract class AbstractSitemap { - const MAX_URLS = 50000; + public const MAX_URLS = 50000; /** * Get the root node name for the sitemap (e.g. 'urlset'). - * @return string */ - abstract protected function getRootNodeName(); + abstract protected function getRootNodeName(): string; /** * Get the node name for the sitemap (e.g. 'url'). - * @return string */ - abstract protected function getNodeName(); + abstract protected function getNodeName(): string; /** * @var string @@ -46,12 +34,12 @@ abstract protected function getNodeName(); protected $xmlNamespaceUri = 'http://www.sitemaps.org/schemas/sitemap/0.9'; /** - * @var DOMDocument + * @var \DOMDocument */ protected $document; /** - * @var DOMElement + * @var \DOMElement */ protected $rootNode; @@ -70,7 +58,7 @@ abstract protected function getNodeName(); */ public function __construct() { - $this->document = new DOMDocument($this->xmlVersion, $this->xmlEncoding); + $this->document = new \DOMDocument($this->xmlVersion, $this->xmlEncoding); $this->rootNode = $this->document->createElementNS($this->xmlNamespaceUri, $this->getRootNodeName()); // Make the output Pretty @@ -80,49 +68,45 @@ public function __construct() /** * Freeze the sitemap, and append the rootNode to the document. */ - public function freeze() + public function freeze(): void { $this->document->appendChild($this->rootNode); $this->isFrozen = true; } - public function isFrozen() + public function isFrozen(): bool { return $this->isFrozen; } /** * Gets the number of Urls in the sitemap. - * @return int */ - public function getUrlCount() + public function getUrlCount(): int { return $this->urlCount; } /** * Checks if the sitemap contains the maximum URL count. - * @return bool */ - public function hasMaxUrlCount() + public function hasMaxUrlCount(): bool { return $this->urlCount === static::MAX_URLS; } /** * Converts the Sitemap to an XML string. - * @return string */ - public function toString() + public function toString(): string { return (string) $this; } /** * Converts the Sitemap to an XML string. - * @return string */ - public function __toString() + public function __toString(): string { if (!$this->isFrozen()) { $this->freeze(); @@ -133,11 +117,10 @@ public function __toString() /** * Adds a URL to the document with the given array of elements. - * @param array $urlArray - * @return $this + * * @throws MaxUrlCountExceededException */ - protected function addUrlToDocument(array $urlArray) + protected function addUrlToDocument(array $urlArray): self { if ($this->hasMaxUrlCount()) { throw new MaxUrlCountExceededException('Maximum number of URLs has been reached, cannot add more.'); @@ -149,7 +132,7 @@ protected function addUrlToDocument(array $urlArray) if (is_null($value)) { continue; } - $node->appendChild(new DOMElement($key, $value)); + $node->appendChild(new \DOMElement($key, $value)); } $this->rootNode->appendChild($node); $this->urlCount++; @@ -160,9 +143,8 @@ protected function addUrlToDocument(array $urlArray) /** * Escapes a string so it can be inserted into the Sitemap * @param string $string The string to escape. - * @return string */ - protected function escapeString($string) + protected function escapeString(string $string): string { $from = ['&', '\'', '"', '>', '<']; $to = ['&', ''', '"', '>', '<']; @@ -172,23 +154,22 @@ protected function escapeString($string) /** * Takes a date as a string (or int in the case of a unix timestamp). - * @param string $dateString - * @return string - * @throws InvalidArgumentException + * + * @throws \InvalidArgumentException */ - protected function formatDate($dateString) + protected function formatDate(string $dateString): string { try { // We have to handle timestamps a little differently if (is_numeric($dateString) && (int) $dateString == $dateString) { - $date = DateTime::createFromFormat('U', (int) $dateString, new DateTimeZone('UTC')); + $date = \DateTime::createFromFormat('U', (int) $dateString, new \DateTimeZone('UTC')); } else { - $date = new DateTime($dateString, new DateTimeZone('UTC')); + $date = new \DateTime($dateString, new \DateTimeZone('UTC')); } - return $date->format(DateTime::W3C); + return $date->format(\DateTimeInterface::W3C); } catch (\Exception $e) { - throw new InvalidArgumentException("Malformed last modified date: {$dateString}", 0, $e); + throw new \InvalidArgumentException("Malformed last modified date: {$dateString}", 0, $e); } } } diff --git a/src/ChangeFrequency.php b/src/ChangeFrequency.php index c83a665..611f113 100644 --- a/src/ChangeFrequency.php +++ b/src/ChangeFrequency.php @@ -4,11 +4,11 @@ class ChangeFrequency { - const ALWAYS = 'always'; - const HOURLY = 'hourly'; - const DAILY = 'daily'; - const WEEKLY = 'weekly'; - const MONTHLY = 'monthly'; - const YEARLY = 'yearly'; - const NEVER = 'never'; + public const ALWAYS = 'always'; + public const HOURLY = 'hourly'; + public const DAILY = 'daily'; + public const WEEKLY = 'weekly'; + public const MONTHLY = 'monthly'; + public const YEARLY = 'yearly'; + public const NEVER = 'never'; } diff --git a/src/Exception/MaxUrlCountExceededException.php b/src/Exception/MaxUrlCountExceededException.php new file mode 100644 index 0000000..b966a45 --- /dev/null +++ b/src/Exception/MaxUrlCountExceededException.php @@ -0,0 +1,7 @@ +escapeString($loc); $lastmod = !is_null($lastmod) ? $this->formatDate($lastmod) : null; diff --git a/src/SitemapFactory.php b/src/SitemapFactory.php index 1beba7c..9f2638b 100644 --- a/src/SitemapFactory.php +++ b/src/SitemapFactory.php @@ -2,11 +2,7 @@ namespace Tackk\Cartographer; -use ArrayObject; -use DateTime; -use Iterator; use League\Flysystem\FilesystemInterface; -use RuntimeException; class SitemapFactory { @@ -33,22 +29,15 @@ public function __construct(FilesystemInterface $filesystem) $this->filesystem = $filesystem; } - /** - * Gets the Filesystem. - * @return FilesystemInterface - */ - public function getFilesystem() + public function getFilesystem(): FilesystemInterface { return $this->filesystem; } /** * Sets the Base URL for sitemap files. - * - * @param string $baseUrl - * @return $this */ - public function setBaseUrl($baseUrl) + public function setBaseUrl(string $baseUrl): self { $this->baseUrl = rtrim($baseUrl, '/'); @@ -57,32 +46,31 @@ public function setBaseUrl($baseUrl) /** * Gets the Base URL for sitemap files. - * @return string */ - public function getBaseUrl() + public function getBaseUrl(): string { return $this->baseUrl; } /** * Gets the array of files created. - * @return array */ - public function getFilesCreated() + public function getFilesCreated(): array { return $this->filesCreated; } /** * Generates the sitemap(s) using the iterator previously set. - * @param \Iterator $iterator - * @throws \RuntimeException + * * @return string The URL for the entry Sitemap + * + * @throws \RuntimeException */ - public function createSitemap(Iterator $iterator) + public function createSitemap(\Iterator $iterator): string { $groupName = $this->randomHash(); - $paths = new ArrayObject(); + $paths = new \ArrayObject(); $sitemap = new Sitemap(); foreach ($iterator as $entry) { if ($sitemap->hasMaxUrlCount()) { @@ -104,15 +92,14 @@ public function createSitemap(Iterator $iterator) /** * Creates a Sitemap index given an Iterator of Sitemaps - * @param Iterator $sitemaps * @return mixed */ - public function createSitemapIndex(Iterator $sitemaps) + public function createSitemapIndex(\Iterator $sitemaps) { $groupName = $this->randomHash(); - $sitemapIndexes = new ArrayObject(); + $sitemapIndexes = new \ArrayObject(); $sitemapIndex = new SitemapIndex(); - $lastmod = date(DateTime::W3C); + $lastmod = date(\DateTimeInterface::W3C); foreach ($sitemaps as $sitemapPath) { // Ignoring because this is an edge case for HUGE sites...like Facebook. // @codeCoverageIgnoreStart @@ -141,11 +128,10 @@ public function createSitemapIndex(Iterator $sitemaps) /** * Writes the given sitemap to the filesystem. The filename pattern is: * {MD5_Hash}.{Class_Name}.{Index}.xml - * @param string $groupName - * @param AbstractSitemap $sitemap + * * @return string The filename of the sitemap written */ - protected function writeSitemap($groupName, AbstractSitemap $sitemap) + protected function writeSitemap(string $groupName, AbstractSitemap $sitemap): string { static $index = 0; @@ -160,11 +146,12 @@ protected function writeSitemap($groupName, AbstractSitemap $sitemap) /** * Parses the given Entry into its constituent parts. + * * @param mixed $entry The entry to parse - * @return array + * * @throws \InvalidArgumentException */ - protected function parseEntry($entry) + protected function parseEntry($entry): array { if (!get_property($entry, 'url')) { throw new \InvalidArgumentException('Url is missing or not accessible.'); @@ -179,32 +166,32 @@ protected function parseEntry($entry) /** * Generates a random MD5 hash. - * @return string + * * @throws \RuntimeException */ - protected function randomHash() + protected function randomHash(): string { return md5($this->randomBytes(32)); } /** * Gets the Full URL for the given file. - * @param string $file - * @return string */ - protected function fileUrl($file) + protected function fileUrl(string $file): string { return $this->baseUrl.'/'.ltrim($file, '/'); } /** * Generates a string of random bytes (of given length). + * * @param integer $bytes The number of bytes to return. + * * @throws \RuntimeException - * @return string + * * @codeCoverageIgnore */ - protected function randomBytes($bytes = 32) + protected function randomBytes(int $bytes = 32): string { if (extension_loaded('openssl')) { return openssl_random_pseudo_bytes($bytes); @@ -212,6 +199,6 @@ protected function randomBytes($bytes = 32) return mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); } - throw new RuntimeException('Extension "openssl" or "mcrypt" is required, but is not installed.'); + throw new \RuntimeException('Extension "openssl" or "mcrypt" is required, but is not installed.'); } } diff --git a/src/SitemapIndex.php b/src/SitemapIndex.php index 6bf0061..5cde9f0 100644 --- a/src/SitemapIndex.php +++ b/src/SitemapIndex.php @@ -4,23 +4,20 @@ class SitemapIndex extends AbstractSitemap { - protected function getRootNodeName() + protected function getRootNodeName(): string { return 'sitemapindex'; } - protected function getNodeName() + protected function getNodeName(): string { return 'sitemap'; } /** * Adds the URL to the sitemapindex. - * @param string $loc - * @param string|int $lastmod - * @return $this */ - public function add($loc, $lastmod) + public function add(string $loc, string $lastmod = null): self { $loc = $this->escapeString($loc); $lastmod = !is_null($lastmod) ? $this->formatDate($lastmod) : null; diff --git a/src/functions.php b/src/functions.php index 11d6b88..4359d18 100644 --- a/src/functions.php +++ b/src/functions.php @@ -6,13 +6,15 @@ /** * Gets the property of the given array or object. + * * @param mixed $entry - * @param string $prop - * @param null $default + * @param mixed|null $default + * * @return mixed + * * @throws InvalidArgumentException */ -function get_property($entry, $prop, $default = null) +function get_property($entry, string $prop, $default = null) { checktype($entry, ['array', 'object', 'ArrayAccess']); @@ -33,11 +35,10 @@ function get_property($entry, $prop, $default = null) * checktype('foo', ['array', 'ArrayAccess']); * * @param mixed $value - * @param array $validTypes - * @return bool + * * @throws \InvalidArgumentException */ -function checktype($value, array $validTypes) +function checktype($value, array $validTypes): bool { $nativeTypes = [ 'array' => 'is_array', diff --git a/tests/AbstractSitemapTest.php b/tests/AbstractSitemapTest.php index 168c4f0..0130b91 100644 --- a/tests/AbstractSitemapTest.php +++ b/tests/AbstractSitemapTest.php @@ -1,28 +1,29 @@ abstractMock = new MockAbstractSitemap(); } @@ -41,7 +42,7 @@ public function testFormatDateWithUnixTimestamp() public function testFormatDateWithInvalidDate() { - $this->setExpectedException('InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->callProtectedMethod('formatDate', ['foo']); } @@ -71,7 +72,7 @@ public function testMaxUrlCount() $urlCount->setAccessible(true); $urlCount->setValue($this->abstractMock, AbstractSitemap::MAX_URLS); - $this->setExpectedException('Tackk\Cartographer\MaxUrlCountExceededException'); + $this->expectException(Tackk\Cartographer\Exception\MaxUrlCountExceededException::class); $this->callProtectedMethod('addUrlToDocument', [['loc' => 'http://foo.com']]); } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 5179fbf..31a1d27 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -1,9 +1,10 @@ setExpectedException('InvalidArgumentException', - 'Invalid type: string, Expected type(s): array, object, ArrayAccess'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid type: string, Expected type(s): array, object, ArrayAccess'); Cartographer\get_property('foo', 'foo'); } @@ -45,8 +46,8 @@ public function testCheckTypePasses() public function testCheckTypeFails() { - $this->setExpectedException('InvalidArgumentException', - 'Invalid type: ArrayObject, Expected type(s): array'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid type: ArrayObject, Expected type(s): array'); Cartographer\checktype(new ArrayObject(), ['array']); } } diff --git a/tests/SitemapFactoryTest.php b/tests/SitemapFactoryTest.php index 0e06375..8669cfa 100644 --- a/tests/SitemapFactoryTest.php +++ b/tests/SitemapFactoryTest.php @@ -1,8 +1,9 @@ filesystem = new Filesystem($adapter); @@ -34,10 +35,10 @@ public function testGetFilesystem() public function testSetBaseUrl() { $this->factory->setBaseUrl('foo/'); - $this->assertAttributeEquals('foo', 'baseUrl', $this->factory); + $this->assertSame('foo', $this->factory->getBaseUrl()); $this->factory->setBaseUrl('foo'); - $this->assertAttributeEquals('foo', 'baseUrl', $this->factory); + $this->assertSame('foo', $this->factory->getBaseUrl()); } public function testGetBaseUrl() @@ -62,7 +63,7 @@ public function testGetFileCreated() public function testCreateRequiresIterator() { - $this->setExpectedException('PHPUnit_Framework_Error'); + $this->expectError(); $this->factory->createSitemap(); } @@ -111,7 +112,8 @@ public function testLargeSitemapCreatesIndex() public function testUrlMustBePresent() { - $this->setExpectedException('InvalidArgumentException', 'Url is missing or not accessible.'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Url is missing or not accessible.'); $this->factory->createSitemap(new ArrayIterator([[]])); } } diff --git a/tests/SitemapIndexTest.php b/tests/SitemapIndexTest.php index 62cc3e2..ee4b1b3 100644 --- a/tests/SitemapIndexTest.php +++ b/tests/SitemapIndexTest.php @@ -1,6 +1,8 @@ Date: Wed, 22 Nov 2023 12:51:21 +0100 Subject: [PATCH 2/2] Update flysystem version --- composer.json | 2 +- src/SitemapFactory.php | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 31d0310..dfa1e6f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ }, "require": { "php": ">=7.3", - "league/flysystem": "^1.0.0", + "league/flysystem": "^3.0", "ext-dom": "*" }, "autoload": { diff --git a/src/SitemapFactory.php b/src/SitemapFactory.php index 9f2638b..31fc489 100644 --- a/src/SitemapFactory.php +++ b/src/SitemapFactory.php @@ -2,12 +2,13 @@ namespace Tackk\Cartographer; -use League\Flysystem\FilesystemInterface; + +use League\Flysystem\FilesystemOperator; class SitemapFactory { /** - * @var FilesystemInterface + * @var FilesystemOperator */ protected $filesystem = null; @@ -22,14 +23,14 @@ class SitemapFactory protected $filesCreated = []; /** - * @param FilesystemInterface $filesystem + * @param FilesystemOperator $filesystem */ - public function __construct(FilesystemInterface $filesystem) + public function __construct(FilesystemOperator $filesystem) { $this->filesystem = $filesystem; } - public function getFilesystem(): FilesystemInterface + public function getFilesystem(): FilesystemOperator { return $this->filesystem; }