From ff03a37231ba54f16dc6eb1ac6fbe80be9f9840b Mon Sep 17 00:00:00 2001 From: Gemorroj Date: Fri, 29 Nov 2019 16:43:18 +0300 Subject: [PATCH] drop php 5.6/7.0 support use type hinting, micro-optimizations --- .php_cs.dist | 4 ++-- .travis.yml | 3 +-- README.md | 10 ++++----- composer.json | 6 +++--- phpunit.xml.dist | 3 +-- src/M3uData.php | 2 +- src/M3uEntry.php | 16 +++++++------- src/M3uParser.php | 22 +++++++++---------- src/Tag/ExtInf.php | 37 +++++++++++++++----------------- src/Tag/ExtTagInterface.php | 16 +++----------- src/Tag/ExtTv.php | 26 +++++++++++----------- src/TagAttributesTrait.php | 20 ++++++++--------- src/TagsManagerTrait.php | 8 +++---- tests/ExceptionTest.php | 5 +++-- tests/ExtCustomTag.php | 8 +++---- tests/M3uDataTest.php | 3 ++- tests/M3uParserTest.php | 3 ++- tests/TagAttributesTraitTest.php | 5 +++-- tests/TagsManagerTraitTest.php | 7 +++--- 19 files changed, 97 insertions(+), 107 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index aa26409..640419c 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -10,9 +10,9 @@ return PhpCsFixer\Config::create() 'array_syntax' => ['syntax' => 'short'], 'combine_consecutive_issets' => true, 'combine_consecutive_unsets' => true, - // 'compact_nullable_typehint' => true, + 'compact_nullable_typehint' => true, 'linebreak_after_opening_tag' => true, - // 'list_syntax' => ['syntax' => 'short'], + 'list_syntax' => ['syntax' => 'short'], // 'mb_str_functions' => true, 'native_function_invocation' => true, 'no_null_property_initialization' => true, diff --git a/.travis.yml b/.travis.yml index cef2351..f415d88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php php: - - 5.6 - - 7.0 - 7.1 - 7.2 - 7.3 + - 7.4 - nightly matrix: diff --git a/README.md b/README.md index 4119974..f91e329 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ### Requirements: -- PHP >= 5.6 +- PHP >= 7.1.3 ### Installation: @@ -166,7 +166,7 @@ class ExtCustomTag implements ExtTagInterface * #EXTCUSTOMTAG:data * @param string $lineStr */ - public function __construct($lineStr = null) + public function __construct(?string $lineStr = null) { if (null !== $lineStr) { $this->makeData($lineStr); @@ -211,7 +211,7 @@ example: /** * @return string */ - public function __toString() + public function __toString(): string { return '#EXTCUSTOMTAG: ' . $this->getData(); } @@ -220,9 +220,9 @@ example: * @param string $lineStr * @return bool */ - public static function isMatch($lineStr) + public static function isMatch(string $lineStr): bool { - return '#EXTCUSTOMTAG:' === \strtoupper(\substr($lineStr, 0, \strlen('#EXTCUSTOMTAG:'))); + return 0 === \stripos($lineStr, '#EXTCUSTOMTAG:'); } } diff --git a/composer.json b/composer.json index 9536eb0..597d61c 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ } ], "require": { - "php": ">=5.6" + "php": "^7.1.3" }, "require-dev": { - "phpunit/phpunit": "^5.7", - "friendsofphp/php-cs-fixer": "^2.15" + "phpunit/phpunit": "^7.5", + "friendsofphp/php-cs-fixer": "^2.16" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7204ee4..d156354 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,7 @@ diff --git a/src/M3uData.php b/src/M3uData.php index dbc70b7..3ed2b9e 100644 --- a/src/M3uData.php +++ b/src/M3uData.php @@ -9,7 +9,7 @@ class M3uData extends \ArrayIterator /** * @return string */ - public function __toString() + public function __toString(): string { $out = '#EXTM3U ' . $this->getAttributesString() . "\n"; diff --git a/src/M3uEntry.php b/src/M3uEntry.php index 2303eaa..0eaed43 100644 --- a/src/M3uEntry.php +++ b/src/M3uEntry.php @@ -22,7 +22,7 @@ class M3uEntry /** * @return ExtTagInterface[] */ - public function getExtTags() + public function getExtTags(): array { return $this->extTags; } @@ -31,7 +31,7 @@ public function getExtTags() * @param ExtTagInterface $extTag * @return $this */ - public function addExtTag(ExtTagInterface $extTag) + public function addExtTag(ExtTagInterface $extTag): self { $this->extTags[] = $extTag; return $this; @@ -42,7 +42,7 @@ public function addExtTag(ExtTagInterface $extTag) * * @return $this */ - public function clearExtTags() + public function clearExtTags(): self { $this->extTags = []; return $this; @@ -51,7 +51,7 @@ public function clearExtTags() /** * @return string */ - public function getPath() + public function getPath(): string { return $this->path; } @@ -60,7 +60,7 @@ public function getPath() * @param string $path * @return $this */ - public function setPath($path) + public function setPath(string $path): self { $this->path = $path; return $this; @@ -69,14 +69,14 @@ public function setPath($path) /** * @return string */ - public function __toString() + public function __toString(): string { $out = ''; foreach ($this->getExtTags() as $extTag) { - $out .= (string)$extTag . $this->lineDelimiter; + $out .= $extTag . $this->lineDelimiter; } - $out .= (string)$this->getPath(); + $out .= $this->getPath(); return \rtrim($out); } diff --git a/src/M3uParser.php b/src/M3uParser.php index 67137bc..2386c78 100644 --- a/src/M3uParser.php +++ b/src/M3uParser.php @@ -9,7 +9,7 @@ class M3uParser /** * @return M3uEntry */ - protected function createM3uEntry() + protected function createM3uEntry(): M3uEntry { return new M3uEntry(); } @@ -17,7 +17,7 @@ protected function createM3uEntry() /** * @return M3uData */ - protected function createM3uData() + protected function createM3uData(): M3uData { return new M3uData(); } @@ -29,7 +29,7 @@ protected function createM3uData() * @throws Exception * @return M3uData entries */ - public function parseFile($file) + public function parseFile(string $file): M3uData { $str = @\file_get_contents($file); if (false === $str) { @@ -45,7 +45,7 @@ public function parseFile($file) * @param string $str * @return M3uData entries */ - public function parse($str) + public function parse(string $str): M3uData { $this->removeBom($str); @@ -79,7 +79,7 @@ public function parse($str) * @param string[] $linesStr * @return M3uEntry */ - protected function parseLine(&$lineNumber, array $linesStr) + protected function parseLine(int &$lineNumber, array $linesStr): M3uEntry { $entry = $this->createM3uEntry(); @@ -112,9 +112,9 @@ protected function parseLine(&$lineNumber, array $linesStr) /** * @param string $str */ - protected function removeBom(&$str) + protected function removeBom(string &$str): void { - if ("\xEF\xBB\xBF" === \substr($str, 0, 3)) { + if (0 === \strpos($str, "\xEF\xBB\xBF")) { $str = \substr($str, 3); } } @@ -123,16 +123,16 @@ protected function removeBom(&$str) * @param string $lineStr * @return bool */ - protected function isExtM3u($lineStr) + protected function isExtM3u(string $lineStr): bool { - return '#EXTM3U' === \strtoupper(\substr($lineStr, 0, 7)); + return 0 === \stripos($lineStr, '#EXTM3U'); } /** * @param string $lineStr * @return bool */ - protected function isComment($lineStr) + protected function isComment(string $lineStr): bool { $matched = false; foreach ($this->getTags() as $availableTag) { @@ -142,6 +142,6 @@ protected function isComment($lineStr) } } - return '#' === \substr($lineStr, 0, 1) && !$matched && !static::isExtM3u($lineStr); + return !$matched && 0 === \strpos($lineStr, '#') && !$this->isExtM3u($lineStr); } } diff --git a/src/Tag/ExtInf.php b/src/Tag/ExtInf.php index 8494c20..4a026f8 100644 --- a/src/Tag/ExtInf.php +++ b/src/Tag/ExtInf.php @@ -22,7 +22,7 @@ class ExtInf implements ExtTagInterface * * @param string $lineStr */ - public function __construct($lineStr = null) + public function __construct(?string $lineStr = null) { if (null !== $lineStr) { $this->make($lineStr); @@ -33,7 +33,7 @@ public function __construct($lineStr = null) * @param string $lineStr * @see http://l189-238-14.cn.ru/api-doc/m3u-extending.html */ - protected function make($lineStr) + protected function make(string $lineStr): void { /* EXTINF format: @@ -41,24 +41,21 @@ protected function make($lineStr) example: #EXTINF:-1 tvg-name=Первый_HD tvg-logo="Первый канал" deinterlace=4 group-title="Эфирные каналы",Первый канал HD */ - $tmp = \substr($lineStr, 8); + $dataLineStr = \substr($lineStr, 8); // Parse duration and title with regex - preg_match('/^(-?\d+)\s*(?:(?:[^=]+=["\'][^"\']*["\'])|(?:[^=]+=[^ ]*))*,(.*)$/', $tmp, $matches); + \preg_match('/^(-?\d+)\s*(?:(?:[^=]+=["\'][^"\']*["\'])|(?:[^=]+=[^ ]*))*,(.*)$/', $dataLineStr, $matches); - $duration = (int)$matches[1]; - $title = \trim($matches[2]); - - $this->setTitle($title); - $this->setDuration($duration); + $this->setDuration((int)$matches[1]); + $this->setTitle(\trim($matches[2])); // Attributes are remaining string after remove duration and title - $attributes = preg_replace('#^'.preg_quote($matches[1]).'(.*)'.preg_quote($matches[2]).'$#', '$1', $tmp); + $attributes = \preg_replace('/^'.\preg_quote($matches[1], '/').'(.*)'.\preg_quote($matches[2], '/').'$/', '$1', $dataLineStr); $splitAttributes = \explode(' ', $attributes, 2); - if (isset($splitAttributes[1]) && \trim($splitAttributes[1])) { - $this->initAttributes(\trim($splitAttributes[1])); + if (isset($splitAttributes[1]) && $trimmedAttributes = \trim($splitAttributes[1])) { + $this->initAttributes($trimmedAttributes); } } @@ -66,7 +63,7 @@ protected function make($lineStr) * @param string $title * @return $this */ - public function setTitle($title) + public function setTitle(string $title): self { $this->title = $title; @@ -78,7 +75,7 @@ public function setTitle($title) * * @return string */ - public function getTitle() + public function getTitle(): string { return $this->title; } @@ -87,7 +84,7 @@ public function getTitle() * @param int $duration * @return $this */ - public function setDuration($duration) + public function setDuration(int $duration): self { $this->duration = $duration; @@ -99,7 +96,7 @@ public function setDuration($duration) * * @return int */ - public function getDuration() + public function getDuration(): int { return $this->duration; } @@ -107,17 +104,17 @@ public function getDuration() /** * @return string */ - public function __toString() + public function __toString(): string { - return '#EXTINF: ' . (int)$this->getDuration() . ' ' . $this->getAttributesString() . ', ' . $this->getTitle(); + return '#EXTINF: ' . $this->getDuration() . ' ' . $this->getAttributesString() . ', ' . $this->getTitle(); } /** * @param string $lineStr * @return bool */ - public static function isMatch($lineStr) + public static function isMatch(string $lineStr): bool { - return '#EXTINF:' === \strtoupper(\substr($lineStr, 0, 8)); + return 0 === \stripos($lineStr, '#EXTINF:'); } } diff --git a/src/Tag/ExtTagInterface.php b/src/Tag/ExtTagInterface.php index e443950..cb8da19 100644 --- a/src/Tag/ExtTagInterface.php +++ b/src/Tag/ExtTagInterface.php @@ -4,19 +4,9 @@ interface ExtTagInterface { - /** - * @param string|null $lineStr - */ - public function __construct($lineStr = null); + public function __construct(?string $lineStr = null); - /** - * @return string - */ - public function __toString(); + public function __toString(): string; - /** - * @param string $lineStr - * @return bool - */ - public static function isMatch($lineStr); + public static function isMatch(string $lineStr): bool; } diff --git a/src/Tag/ExtTv.php b/src/Tag/ExtTv.php index 3b3f721..cad1994 100644 --- a/src/Tag/ExtTv.php +++ b/src/Tag/ExtTv.php @@ -25,7 +25,7 @@ class ExtTv implements ExtTagInterface * #EXTTV:nacionalni,hd;slovenski;SLO1;http://cdn1.siol.tv/logo/93x78/slo2.png * @param string $lineStr */ - public function __construct($lineStr = null) + public function __construct(?string $lineStr = null) { if (null !== $lineStr) { $this->makeData($lineStr); @@ -36,7 +36,7 @@ public function __construct($lineStr = null) * @param string $lineStr * @see https://github.com/Gemorroj/M3uParser/issues/5 */ - protected function makeData($lineStr) + protected function makeData(string $lineStr): void { /* EXTTV format: @@ -59,7 +59,7 @@ protected function makeData($lineStr) /** * @return string[] */ - public function getTags() + public function getTags(): array { return $this->tags; } @@ -68,7 +68,7 @@ public function getTags() * @param string[] $tags * @return $this */ - public function setTags(array $tags) + public function setTags(array $tags): self { $this->tags = $tags; return $this; @@ -77,7 +77,7 @@ public function setTags(array $tags) /** * @return string */ - public function getLanguage() + public function getLanguage(): string { return $this->language; } @@ -86,7 +86,7 @@ public function getLanguage() * @param string $language * @return $this */ - public function setLanguage($language) + public function setLanguage(string $language): self { $this->language = $language; return $this; @@ -95,7 +95,7 @@ public function setLanguage($language) /** * @return string */ - public function getXmlTvId() + public function getXmlTvId(): string { return $this->xmlTvId; } @@ -104,7 +104,7 @@ public function getXmlTvId() * @param string $xmlTvId * @return $this */ - public function setXmlTvId($xmlTvId) + public function setXmlTvId(string $xmlTvId): self { $this->xmlTvId = $xmlTvId; return $this; @@ -113,7 +113,7 @@ public function setXmlTvId($xmlTvId) /** * @return null|string */ - public function getIconUrl() + public function getIconUrl(): ?string { return $this->iconUrl; } @@ -122,7 +122,7 @@ public function getIconUrl() * @param string $iconUrl * @return $this */ - public function setIconUrl($iconUrl) + public function setIconUrl(?string $iconUrl): self { $this->iconUrl = $iconUrl; return $this; @@ -131,7 +131,7 @@ public function setIconUrl($iconUrl) /** * @return string */ - public function __toString() + public function __toString(): string { return '#EXTTV: ' . \implode(',', $this->getTags()) . ';' . $this->getLanguage() . ';' . $this->getXmlTvId() . ($this->getIconUrl() ? ';' . $this->getIconUrl() : ''); } @@ -140,8 +140,8 @@ public function __toString() * @param string $lineStr * @return bool */ - public static function isMatch($lineStr) + public static function isMatch(string $lineStr): bool { - return '#EXTTV:' === \strtoupper(\substr($lineStr, 0, 7)); + return 0 === \stripos($lineStr, '#EXTTV:'); } } diff --git a/src/TagAttributesTrait.php b/src/TagAttributesTrait.php index 549ef2a..89d2dd7 100644 --- a/src/TagAttributesTrait.php +++ b/src/TagAttributesTrait.php @@ -14,7 +14,7 @@ trait TagAttributesTrait * * @param string $attrString */ - public function initAttributes($attrString) + public function initAttributes(string $attrString): void { $this->parseQuotedAttributes($attrString); $this->parseNotQuotedAttributes($attrString); @@ -23,7 +23,7 @@ public function initAttributes($attrString) /** * @param string $attrString */ - private function parseQuotedAttributes($attrString) + private function parseQuotedAttributes(string $attrString): void { \preg_match_all('/([a-zA-Z0-9\-]+)="([^"]*)"/', $attrString, $matches, \PREG_SET_ORDER); @@ -36,7 +36,7 @@ private function parseQuotedAttributes($attrString) /** * @param string $attrString */ - private function parseNotQuotedAttributes($attrString) + private function parseNotQuotedAttributes(string $attrString): void { \preg_match_all('/([a-zA-Z0-9\-]+)=([^ "]+)/', $attrString, $matches, \PREG_SET_ORDER); @@ -48,7 +48,7 @@ private function parseNotQuotedAttributes($attrString) /** * @return array */ - public function getAttributes() + public function getAttributes(): array { return $this->attributes; } @@ -57,16 +57,16 @@ public function getAttributes() * @param string $name * @return string|null */ - public function getAttribute($name) + public function getAttribute(string $name): ?string { - return isset($this->attributes[$name]) ? $this->attributes[$name] : null; + return $this->attributes[$name] ?? null; } /** * @param string $name * @return bool */ - public function hasAttribute($name) + public function hasAttribute(string $name): bool { return isset($this->attributes[$name]); } @@ -75,7 +75,7 @@ public function hasAttribute($name) * @param array $attributes * @return $this */ - public function setAttributes(array $attributes) + public function setAttributes(array $attributes): self { $this->attributes = $attributes; return $this; @@ -86,7 +86,7 @@ public function setAttributes(array $attributes) * @param string $value * @return $this */ - public function setAttribute($name, $value) + public function setAttribute(string $name, string $value): self { $this->attributes[$name] = $value; return $this; @@ -95,7 +95,7 @@ public function setAttribute($name, $value) /** * @return string */ - protected function getAttributesString() + protected function getAttributesString(): string { $out = ''; diff --git a/src/TagsManagerTrait.php b/src/TagsManagerTrait.php index ed442b7..4bbbd5a 100644 --- a/src/TagsManagerTrait.php +++ b/src/TagsManagerTrait.php @@ -20,7 +20,7 @@ trait TagsManagerTrait * @throws Exception * @return $this */ - public function addTag($tag) + public function addTag(string $tag): self { if (!\in_array(ExtTagInterface::class, \class_implements($tag), true)) { throw new Exception(\sprintf('The class %s must be implement interface %s', $tag, ExtTagInterface::class)); @@ -36,7 +36,7 @@ public function addTag($tag) * @throws Exception * @return $this */ - public function addDefaultTags() + public function addDefaultTags(): self { $this->addTag(ExtInf::class); $this->addTag(ExtTv::class); @@ -48,7 +48,7 @@ public function addDefaultTags() * * @return $this */ - public function clearTags() + public function clearTags(): self { $this->tags = []; return $this; @@ -59,7 +59,7 @@ public function clearTags() * * @return string[] */ - protected function getTags() + protected function getTags(): array { return $this->tags; } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index f2a10d6..0d091ef 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -2,10 +2,11 @@ namespace M3uParser\Tests; use M3uParser\Exception; +use PHPUnit\Framework\TestCase; -class ExceptionTest extends \PHPUnit_Framework_TestCase +class ExceptionTest extends TestCase { - public function test() + public function test(): void { self::assertInstanceOf('Exception', new Exception); } diff --git a/tests/ExtCustomTag.php b/tests/ExtCustomTag.php index d48e848..c9ddeda 100644 --- a/tests/ExtCustomTag.php +++ b/tests/ExtCustomTag.php @@ -15,7 +15,7 @@ class ExtCustomTag implements ExtTagInterface * #EXTCUSTOMTAG:data * @param string $lineStr */ - public function __construct($lineStr = null) + public function __construct(?string $lineStr = null) { if (null !== $lineStr) { $this->makeData($lineStr); @@ -60,7 +60,7 @@ public function setData($data) /** * @return string */ - public function __toString() + public function __toString(): string { return '#EXTCUSTOMTAG: ' . $this->getData(); } @@ -69,8 +69,8 @@ public function __toString() * @param string $lineStr * @return bool */ - public static function isMatch($lineStr) + public static function isMatch(string $lineStr): bool { - return '#EXTCUSTOMTAG:' === \strtoupper(\substr($lineStr, 0, \strlen('#EXTCUSTOMTAG:'))); + return 0 === \stripos($lineStr, '#EXTCUSTOMTAG:'); } } diff --git a/tests/M3uDataTest.php b/tests/M3uDataTest.php index b757fe9..2ea2b97 100644 --- a/tests/M3uDataTest.php +++ b/tests/M3uDataTest.php @@ -5,8 +5,9 @@ use M3uParser\M3uEntry; use M3uParser\Tag\ExtInf; use M3uParser\Tag\ExtTv; +use PHPUnit\Framework\TestCase; -class M3uDataTest extends \PHPUnit_Framework_TestCase +class M3uDataTest extends TestCase { public function testEntryToString() { diff --git a/tests/M3uParserTest.php b/tests/M3uParserTest.php index e02969d..10a8662 100644 --- a/tests/M3uParserTest.php +++ b/tests/M3uParserTest.php @@ -8,8 +8,9 @@ use M3uParser\Tag\ExtInf; use M3uParser\Tag\ExtTagInterface; use M3uParser\Tag\ExtTv; +use PHPUnit\Framework\TestCase; -class M3uParserTest extends \PHPUnit_Framework_TestCase +class M3uParserTest extends TestCase { public function testParseFileFail() { diff --git a/tests/TagAttributesTraitTest.php b/tests/TagAttributesTraitTest.php index ae56ef4..1d609bb 100644 --- a/tests/TagAttributesTraitTest.php +++ b/tests/TagAttributesTraitTest.php @@ -2,8 +2,9 @@ namespace M3uParser\Tests; use M3uParser\TagAttributesTrait; +use PHPUnit\Framework\TestCase; -class TagAttributesTraitTest extends \PHPUnit_Framework_TestCase +class TagAttributesTraitTest extends TestCase { use TagAttributesTrait; @@ -13,7 +14,7 @@ public function testAttributes() $result = $this->getAttributes(); - self::assertInternalType('array', $result); + self::assertIsArray($result); self::assertEquals([ 'tvg-ID' => '', 'tvg-name' => 'MEDI 1 SAT', diff --git a/tests/TagsManagerTraitTest.php b/tests/TagsManagerTraitTest.php index bfceb0b..41c7fb9 100644 --- a/tests/TagsManagerTraitTest.php +++ b/tests/TagsManagerTraitTest.php @@ -4,8 +4,9 @@ use M3uParser\Exception; use M3uParser\Tag\ExtInf; use M3uParser\TagsManagerTrait; +use PHPUnit\Framework\TestCase; -class TagsManagerTraitTest extends \PHPUnit_Framework_TestCase +class TagsManagerTraitTest extends TestCase { use TagsManagerTrait; @@ -15,7 +16,7 @@ public function testTags() $result = $this->getTags(); - self::assertInternalType('array', $result); + self::assertIsArray($result); self::assertEquals([ExtInf::class], $result); } @@ -27,7 +28,7 @@ public function testClearTags() $result = $this->getTags(); - self::assertInternalType('array', $result); + self::assertIsArray($result); self::assertEquals([], $result); }