Skip to content

Commit

Permalink
drop php 5.6/7.0 support
Browse files Browse the repository at this point in the history
use type hinting, micro-optimizations
  • Loading branch information
Gemorroj committed Nov 29, 2019
1 parent e53fa01 commit ff03a37
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- nightly

matrix:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### Requirements:

- PHP >= 5.6
- PHP >= 7.1.3


### Installation:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -211,7 +211,7 @@ example:
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return '#EXTCUSTOMTAG: ' . $this->getData();
}
Expand All @@ -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:');
}
}

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.7/phpunit.xsd"
backupGlobals="false"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.5/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
Expand Down
2 changes: 1 addition & 1 deletion src/M3uData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class M3uData extends \ArrayIterator
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
$out = '#EXTM3U ' . $this->getAttributesString() . "\n";

Expand Down
16 changes: 8 additions & 8 deletions src/M3uEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class M3uEntry
/**
* @return ExtTagInterface[]
*/
public function getExtTags()
public function getExtTags(): array
{
return $this->extTags;
}
Expand All @@ -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;
Expand All @@ -42,7 +42,7 @@ public function addExtTag(ExtTagInterface $extTag)
*
* @return $this
*/
public function clearExtTags()
public function clearExtTags(): self
{
$this->extTags = [];
return $this;
Expand All @@ -51,7 +51,7 @@ public function clearExtTags()
/**
* @return string
*/
public function getPath()
public function getPath(): string
{
return $this->path;
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
22 changes: 11 additions & 11 deletions src/M3uParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class M3uParser
/**
* @return M3uEntry
*/
protected function createM3uEntry()
protected function createM3uEntry(): M3uEntry
{
return new M3uEntry();
}

/**
* @return M3uData
*/
protected function createM3uData()
protected function createM3uData(): M3uData
{
return new M3uData();
}
Expand All @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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) {
Expand All @@ -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);
}
}
37 changes: 17 additions & 20 deletions src/Tag/ExtInf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -33,40 +33,37 @@ 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:
#EXTINF:<duration> [<attributes-list>], <title>
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);
}
}

/**
* @param string $title
* @return $this
*/
public function setTitle($title)
public function setTitle(string $title): self
{
$this->title = $title;

Expand All @@ -78,7 +75,7 @@ public function setTitle($title)
*
* @return string
*/
public function getTitle()
public function getTitle(): string
{
return $this->title;
}
Expand All @@ -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;

Expand All @@ -99,25 +96,25 @@ public function setDuration($duration)
*
* @return int
*/
public function getDuration()
public function getDuration(): int
{
return $this->duration;
}

/**
* @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:');
}
}
16 changes: 3 additions & 13 deletions src/Tag/ExtTagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading

0 comments on commit ff03a37

Please sign in to comment.