1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2024-11-25 20:48:51 +01:00

🚨 Clean code for linters

This commit is contained in:
Clement Desmidt 2023-04-12 15:28:38 +02:00
parent 08138992fd
commit 3419e7d46c
23 changed files with 185 additions and 260 deletions

View File

@ -18,6 +18,7 @@
"phpunit/phpunit": "^9" "phpunit/phpunit": "^9"
}, },
"require": { "require": {
"php": ">=8.0",
"ext-dom": "*", "ext-dom": "*",
"ext-libxml": "*" "ext-libxml": "*"
} }

View File

@ -6,14 +6,19 @@ use DOMDocument;
use DOMElement; use DOMElement;
use Shikiryu\SRSS\Entity\Channel; use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Item; use Shikiryu\SRSS\Entity\Item;
use Shikiryu\SRSS\Exception\DOMBuilderException;
use Shikiryu\SRSS\SRSS; use Shikiryu\SRSS\SRSS;
use Shikiryu\SRSS\SRSSTools;
class SRSSBuilder extends DomDocument class SRSSBuilder extends DomDocument
{ {
private function buildRSS(SRSS $srss) /**
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
*/
private function buildRSS(SRSS $srss): SRSSBuilder
{ {
try {
$root = $this->createElement('rss'); $root = $this->createElement('rss');
$root->setAttribute('version', '2.0'); $root->setAttribute('version', '2.0');
$channel = $this->createElement('channel'); $channel = $this->createElement('channel');
@ -30,141 +35,50 @@ class SRSSBuilder extends DomDocument
$this->preserveWhiteSpace = false; $this->preserveWhiteSpace = false;
// $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html'; // $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html';
} catch (\DOMException $e) {
throw new DOMBuilderException($e);
}
return $this; return $this;
} }
public function build(SRSS $srss, string $filepath)
{
$this->buildRSS($srss);
$this->save($filepath); /**
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
*/
public function build(SRSS $srss, string $filepath): void
{
$this
->buildRSS($srss)
->save($filepath);
} }
/** /**
* @return false|string * @return false|string
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
*/ */
public function show(SRSS $srss) public function show(SRSS $srss): bool|string
{ {
$this->buildRSS($srss); return $this
->buildRSS($srss)
return $this->saveXml(); ->saveXml();
} }
/** private function appendChannelToDom(Channel $channel, DOMElement $node): void
* setter of "image"'s channel attributes
* @param $url string picture's url
* @param $title string picture's title
* @param $link string link on the picture
* @param $width int width
* @param $height int height
* @param $description string description
* TODO
*/
public function setImage($url, $title, $link, $width = 0, $height = 0, $description = '')
{ {
$channel = $this->_getChannel(); foreach (array_filter($channel->toArray(), static fn($el) => !empty($el)) as $name => $value) {
$array = [];
$url = SRSSTools::checkLink($url);
$array['url'] = $url;
$title = SRSSTools::noHTML($title);
$array['title'] = $title;
$link = SRSSTools::checkLink($link);
$array['link'] = $link;
if($width != 0)
{
$width = SRSSTools::checkInt($width);
$array['width'] = $width;
}
if($height != 0)
{
$height = SRSSTools::checkInt($height);
$array['height'] = $height;
}
if($description != 0)
{
$description = SRSSTools::noHTML($description);
$array['description'] = $description;
}
if($this->image == null)
{
$node = $this->createElement('image');
$urlNode = $this->createElement('url', $url);
$titleNode = $this->createElement('title', $title);
$linkNode = $this->createElement('link', $link);
$node->appendChild($urlNode);
$node->appendChild($titleNode);
$node->appendChild($linkNode);
if($width != 0)
{
$widthNode = $this->createElement('width', $width);
$node->appendChild($widthNode);
}
if($height != 0)
{
$heightNode = $this->createElement('height', $height);
$node->appendChild($heightNode);
}
if($description != '')
{
$descNode = $this->createElement('description', $description);
$node->appendChild($descNode);
}
$channel->appendChild($node);
}
$this->attr['image'] = $array;
}
/**
* setter of "cloud"'s channel attributes
* @param $domain string domain
* @param $port int port
* @param $path string path
* @param $registerProcedure string register procedure
* @param $protocol string protocol
* TODO
*/
public function setCloud($domain, $port, $path, $registerProcedure, $protocol)
{
$channel = $this->_getChannel();
$array = array();
$domain = SRSSTools::noHTML($domain);
$array['domain'] = $domain;
$port = SRSSTools::checkInt($port);
$array['port'] = $port;
$path = SRSSTools::noHTML($path);
$array['path'] = $path;
$registerProcedure = SRSSTools::noHTML($registerProcedure);
$array['registerProcedure'] = $registerProcedure;
$protocol = SRSSTools::noHTML($protocol);
$array['protocol'] = $protocol;
if($this->cloud == null)
{
$node = $this->createElement('cloud');
$node->setAttribute('domain', $domain);
$node->setAttribute('port', $port);
$node->setAttribute('path', $path);
$node->setAttribute('registerProcedure', $registerProcedure);
$node->setAttribute('protocol', $protocol);
$channel->appendChild($node);
}
$this->attr['cloud'] = $array;
}
private function appendChannelToDom(Channel $channel, DOMElement $node)
{
foreach (array_filter($channel->toArray(), fn($el) => !empty($el)) as $name => $value) {
$new_node = $this->createElement($name, $value); $new_node = $this->createElement($name, $value);
$node->appendChild($new_node); $node->appendChild($new_node);
} }
} }
private function appendItemsToDom(array $items, DOMElement $channel) private function appendItemsToDom(array $items, DOMElement $channel): void
{ {
foreach ($items as $item) { foreach ($items as $item) {
$this->appendItemToDom($item, $channel); $this->appendItemToDom($item, $channel);
} }
} }
private function appendItemToDom(Item $item, DOMElement $channel) private function appendItemToDom(Item $item, DOMElement $channel): void
{ {
$itemNode = $this->createElement('item'); $itemNode = $this->createElement('item');
foreach (array_filter($item->toArray()) as $name => $value) { foreach (array_filter($item->toArray()) as $name => $value) {

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Channel; namespace Shikiryu\SRSS\Entity\Channel;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -21,7 +22,7 @@ class Category extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Channel; namespace Shikiryu\SRSS\Entity\Channel;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -29,13 +30,11 @@ class Cloud extends HasValidator implements SRSSElement
*/ */
public ?string $protocol = null; public ?string $protocol = null;
//<cloud domain="rpc.sys.com" port="80" path="/RPC2" registerProcedure="myCloud.rssPleaseNotify" protocol="xml-rpc" />
public function isValid(): bool public function isValid(): bool
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Channel; namespace Shikiryu\SRSS\Entity\Channel;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -40,7 +41,7 @@ class Image extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity; namespace Shikiryu\SRSS\Entity;
use ReflectionException;
use Shikiryu\SRSS\Entity\Item\Category; use Shikiryu\SRSS\Entity\Item\Category;
use Shikiryu\SRSS\Entity\Item\Enclosure; use Shikiryu\SRSS\Entity\Item\Enclosure;
use Shikiryu\SRSS\Entity\Item\Source; use Shikiryu\SRSS\Entity\Item\Source;
@ -64,7 +65,7 @@ class Item extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Item; namespace Shikiryu\SRSS\Entity\Item;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -21,7 +22,7 @@ class Category extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Item; namespace Shikiryu\SRSS\Entity\Item;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -27,7 +28,7 @@ class Enclosure extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Item; namespace Shikiryu\SRSS\Entity\Item;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -22,7 +23,7 @@ class Source extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Shikiryu\SRSS\Entity\Media; namespace Shikiryu\SRSS\Entity\Media;
use ReflectionException;
use Shikiryu\SRSS\Entity\SRSSElement; use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator; use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator; use Shikiryu\SRSS\Validator\Validator;
@ -69,7 +70,7 @@ class Content extends HasValidator implements SRSSElement
{ {
try { try {
return (new Validator())->isObjectValid($this); return (new Validator())->isObjectValid($this);
} catch (\ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }

View File

@ -0,0 +1,12 @@
<?php
namespace Shikiryu\SRSS\Exception;
class DOMBuilderException extends SRSSException
{
public function __construct(\DOMException $e)
{
parent::__construct(sprintf('Error while building DOM (%s)', $e->getMessage()));
}
}

View File

@ -11,7 +11,10 @@ class SRSSException extends Exception
parent :: __construct($msg); parent :: __construct($msg);
} }
public function getError() /**
* @return string
*/
public function getError(): string
{ {
return 'Une exception a été générée : <strong>Message : ' . $this->getMessage() . '</strong> à la ligne : ' . $this->getLine(); return 'Une exception a été générée : <strong>Message : ' . $this->getMessage() . '</strong> à la ligne : ' . $this->getLine();
} }

View File

@ -95,33 +95,6 @@ class ItemParser extends DomDocument
return $this->getElementsByTagName('item')->item(0); return $this->getElementsByTagName('item')->item(0);
} }
/**
* setter for enclosure's properties
*
* @param $url string url
* @param $length int length
* @param $type string type
* @throws DOMException
*/
public function setEnclosure(string $url, int $length, string $type): void
{
$array = [];
$url = SRSSTools::checkLink($url);
$array['url'] = $url;
$length = SRSSTools::checkInt($length);
$array['length'] = $length;
$type = SRSSTools::noHTML($type);
$array['type'] = $type;
if ($this->enclosure == null) {
$node = $this->createElement('enclosure');
$node->setAttribute('url', $url);
$node->setAttribute('length', $length);
$node->setAttribute('type', $type);
$this->node->appendChild($node);
}
$this->attr['enclosure'] = $array;
}
/** /**
* check if current item is valid (following specifications) * check if current item is valid (following specifications)
* @return bool * @return bool
@ -156,12 +129,13 @@ class ItemParser extends DomDocument
} }
$flag = $this->possibilities[$name]; $flag = $this->possibilities[$name];
if ($flag !== '') if ($flag !== '') {
$val = SRSSTools::check($val, $flag); $val = SRSSTools::check($val, $flag);
}
if (!empty($val)) { if (!empty($val)) {
if ($val instanceof DOMElement) { if ($val instanceof DOMElement) {
$this->node->appendChild($val); $this->node->appendChild($val);
} elseif ($this->$name == null) { } elseif ($this->$name === null) {
$this->node->appendChild(new DomElement($name, $val)); $this->node->appendChild(new DomElement($name, $val));
} }
$this->attr[$name] = $val; $this->attr[$name] = $val;
@ -178,11 +152,17 @@ class ItemParser extends DomDocument
*/ */
public function __get($name) public function __get($name)
{ {
if (isset($this->attr[$name])) if (isset($this->attr[$name])) {
return $this->attr[$name]; return $this->attr[$name];
}
if (array_key_exists($name, $this->possibilities)) { if (array_key_exists($name, $this->possibilities)) {
$tmp = $this->node->getElementsByTagName($name); $tmp = $this->node->getElementsByTagName($name);
if ($tmp->length != 1) return null;
if ($tmp->length !== 1) {
return null;
}
return $tmp->item(0)->nodeValue; return $tmp->item(0)->nodeValue;
} }
@ -194,7 +174,7 @@ class ItemParser extends DomDocument
* transform current item's object into an array * transform current item's object into an array
* @return array * @return array
*/ */
public function toArray() public function toArray(): array
{ {
$infos = []; $infos = [];
foreach ($this->attr as $attrName => $attrVal) { foreach ($this->attr as $attrName => $attrVal) {

View File

@ -5,10 +5,8 @@ namespace Shikiryu\SRSS\Parser;
use DOMDocument; use DOMDocument;
use DOMNode; use DOMNode;
use DOMNodeList; use DOMNodeList;
use DOMXPath;
use Shikiryu\SRSS\Entity\Channel; use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Channel\Image; use Shikiryu\SRSS\Entity\Channel\Image;
use Shikiryu\SRSS\Entity\Item;
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException; use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
use Shikiryu\SRSS\Exception\SRSSException; use Shikiryu\SRSS\Exception\SRSSException;
use Shikiryu\SRSS\Exception\UnreadableRSSException; use Shikiryu\SRSS\Exception\UnreadableRSSException;
@ -17,13 +15,11 @@ use Shikiryu\SRSS\SRSS;
class SRSSParser extends DomDocument class SRSSParser extends DomDocument
{ {
private SRSS $doc; private SRSS $doc;
private DOMXPath $xpath;
public function __construct() public function __construct()
{ {
libxml_use_internal_errors(true); libxml_use_internal_errors(true);
parent::__construct(); parent::__construct();
$this->xpath = new DOMXpath($this);
$this->doc = new SRSS(); $this->doc = new SRSS();
} }
@ -65,10 +61,9 @@ class SRSSParser extends DomDocument
} }
/** /**
* @return Item[]
* @throws SRSSException * @throws SRSSException
*/ */
private function _parseItems(): array private function _parseItems(): void
{ {
$channel = $this->_getChannel(); $channel = $this->_getChannel();
/** @var DOMNodeList $items */ /** @var DOMNodeList $items */
@ -80,9 +75,8 @@ class SRSSParser extends DomDocument
$this->doc->items[$i] = ItemParser::read($items->item($i)); $this->doc->items[$i] = ItemParser::read($items->item($i));
} }
} }
return $this->doc->items;
} }
/** /**
* putting all RSS attributes into the object * putting all RSS attributes into the object
* @throws SRSSException * @throws SRSSException
@ -132,36 +126,10 @@ class SRSSParser extends DomDocument
private function _getChannel(): DOMNode private function _getChannel(): DOMNode
{ {
$channel = $this->getElementsByTagName('channel'); $channel = $this->getElementsByTagName('channel');
if($channel->length != 1) { if($channel->length !== 1) {
throw new ChannelNotFoundInRSSException('channel node not created, or too many channel nodes'); throw new ChannelNotFoundInRSSException('channel node not created, or too many channel nodes');
} }
return $channel->item(0); return $channel->item(0);
} }
/**
* getter of "image"'s channel attributes
* @return string|array
* TODO
*/
public function image()
{
$args = func_get_args();
if (func_num_args() == 0) {
$args[0] = 'url';
}
$img = $this->xpath->query('//channel/image');
if ($img->length != 1) { // <image> is not in channel
return null;
}
$img = $img->item(0);
$r = [];
foreach ($img->childNodes as $child) {
if ($child->nodeType == XML_ELEMENT_NODE && in_array($child->nodeName, $args)) {
$r[$child->nodeName] = $child->nodeValue;
}
}
return (func_num_args() > 1) ? $r : $r[$args[0]];
}
} }

View File

@ -6,6 +6,9 @@ use Iterator;
use ReflectionException; use ReflectionException;
use Shikiryu\SRSS\Builder\SRSSBuilder; use Shikiryu\SRSS\Builder\SRSSBuilder;
use Shikiryu\SRSS\Entity\Channel; use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Channel\Category;
use Shikiryu\SRSS\Entity\Channel\Cloud;
use Shikiryu\SRSS\Entity\Channel\Image;
use Shikiryu\SRSS\Entity\Item; use Shikiryu\SRSS\Entity\Item;
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException; use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
use Shikiryu\SRSS\Exception\PropertyNotFoundException; use Shikiryu\SRSS\Exception\PropertyNotFoundException;
@ -13,6 +16,27 @@ use Shikiryu\SRSS\Exception\SRSSException;
use Shikiryu\SRSS\Exception\UnreadableRSSException; use Shikiryu\SRSS\Exception\UnreadableRSSException;
use Shikiryu\SRSS\Parser\SRSSParser; use Shikiryu\SRSS\Parser\SRSSParser;
/**
* @property null|string $title
* @property null|string $link
* @property null|string $description
* @property null|string $language
* @property null|string $copyright
* @property null|string $managingEditor
* @property null|string $webMaster
* @property null|string $pubDate
* @property null|string $lastBuildDate
* @property null|Category[] $category
* @property null|string $generator
* @property null|string $docs
* @property null|Cloud $cloud
* @property null|string $ttl
* @property null|Image $image
* @property null|string $rating
* @property null|string $textInput
* @property null|string $skipHours
* @property null|string $skipDays
*/
class SRSS implements Iterator class SRSS implements Iterator
{ {
public Channel $channel; public Channel $channel;
@ -72,7 +96,7 @@ class SRSS implements Iterator
} }
return ($valid && $this->channel->isValid()); return ($valid && $this->channel->isValid());
} catch (ReflectionException $e) { } catch (ReflectionException) {
return false; return false;
} }
} }
@ -244,6 +268,7 @@ class SRSS implements Iterator
* @param string $path * @param string $path
* *
* @return void * @return void
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
*/ */
public function save(string $path): void public function save(string $path): void
{ {
@ -252,8 +277,9 @@ class SRSS implements Iterator
/** /**
* @return false|string * @return false|string
* @throws \Shikiryu\SRSS\Exception\DOMBuilderException
*/ */
public function show() public function show(): bool|string
{ {
return (new SRSSBuilder('1.0', 'UTF-8'))->show($this); return (new SRSSBuilder('1.0', 'UTF-8'))->show($this);
} }

View File

@ -28,36 +28,37 @@ class SRSSTools
/** /**
* format the RSS to the wanted format * format the RSS to the wanted format
*
* @param $format string wanted format * @param $format string wanted format
* @param $date string RSS date * @param $date string RSS date
*
* @return string date * @return string date
*/ */
public static function formatDate($format, $date) public static function formatDate(string $format, string $date): string
{ {
return date($format, strtotime($date)); return date($format, strtotime($date));
} }
/** /**
* format a date for RSS format * format a date for RSS format
*
* @param string $date date to format * @param string $date date to format
* @param string $format * @param string $format
*
* @return string * @return string
*/ */
public static function getRSSDate($date, $format='') public static function getRSSDate(string $date, string $format = ''): string
{ {
$datepos = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU'; $date_position = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU';
if($format != '' && preg_match('~^(['.$datepos.']{1})(-|/)(['.$datepos.']{1})(-|/)(['.$datepos.']{1})$~', $format, $match)){ if($format !== '' && preg_match('~^(['.$date_position.']{1})([-/])(['.$date_position.']{1})([-/])(['.$date_position.']{1})$~', $format)){
$sep = $match[2]; $datetime = DateTime::createFromFormat($format, $date);
$format = '%'.$match[1].$sep.'%'.$match[3].$sep.'%'.$match[5]; if ($datetime === false) {
if($dateArray = strptime($date, $format)){
$mois = (int)$dateArray['tm_mon'] + 1;
$annee = strlen($dateArray['tm_year']) > 2 ? '20'.substr($dateArray['tm_year'], -2) : '19'.$dateArray['tm_year'];
$date = $annee.'-'.$mois.'-'.$dateArray['tm_mday'];
return date("D, d M Y H:i:s T", strtotime($date));
}
return ''; return '';
} }
return $datetime->format(DATE_RSS);
}
if (strtotime($date) !==false ) { if (strtotime($date) !==false ) {
return date("D, d M Y H:i:s T", strtotime($date)); return date("D, d M Y H:i:s T", strtotime($date));
} }
@ -73,41 +74,49 @@ class SRSSTools
/** /**
* check if it's an url * check if it's an url
*
* @param $check string to check * @param $check string to check
*
* @return string|boolean the filtered data, or FALSE if the filter fails. * @return string|boolean the filtered data, or FALSE if the filter fails.
*/ */
public static function checkLink($check) public static function checkLink(string $check): bool|string
{ {
return filter_var($check, FILTER_VALIDATE_URL); return filter_var($check, FILTER_VALIDATE_URL);
} }
/** /**
* make a string XML-compatible * make a string XML-compatible
*
* @param $check string to format * @param $check string to format
*
* @return string formatted string * @return string formatted string
* TODO CDATA ? * TODO CDATA ?
*/ */
public static function HTML4XML($check) public static function HTML4XML(string $check): string
{ {
return htmlspecialchars($check); return htmlspecialchars($check);
} }
/** /**
* delete html tags * delete html tags
*
* @param $check string to format * @param $check string to format
*
* @return string formatted string * @return string formatted string
*/ */
public static function noHTML($check) public static function noHTML(string $check): string
{ {
return strip_tags($check); return strip_tags($check);
} }
/** /**
* check if it's a day (in RSS terms) * check if it's a day (in RSS terms)
*
* @param $check string to check * @param $check string to check
*
* @return string the day, or empty string * @return string the day, or empty string
*/ */
public static function checkDay($check) public static function checkDay(string $check): string
{ {
$possibleDay = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']; $possibleDay = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
return in_array(strtolower($check), $possibleDay) ? $check : ''; return in_array(strtolower($check), $possibleDay) ? $check : '';
@ -115,20 +124,24 @@ class SRSSTools
/** /**
* check if it's an email * check if it's an email
*
* @param $check string to check * @param $check string to check
*
* @return string|boolean the filtered data, or FALSE if the filter fails. * @return string|boolean the filtered data, or FALSE if the filter fails.
*/ */
public static function checkEmail($check) public static function checkEmail(string $check): bool|string
{ {
return filter_var($check, FILTER_VALIDATE_EMAIL); return filter_var($check, FILTER_VALIDATE_EMAIL);
} }
/** /**
* check if it's an hour (in RSS terms) * check if it's an hour (in RSS terms)
*
* @param $check string to check * @param $check string to check
*
* @return string|boolean the filtered data, or FALSE if the filter fails. * @return string|boolean the filtered data, or FALSE if the filter fails.
*/ */
public static function checkHour($check) public static function checkHour(string $check): bool|string
{ {
$options = [ $options = [
'options' => [ 'options' => [
@ -142,10 +155,12 @@ class SRSSTools
/** /**
* check if it's an int * check if it's an int
*
* @param $check int to check * @param $check int to check
*
* @return int|boolean the filtered data, or FALSE if the filter fails. * @return int|boolean the filtered data, or FALSE if the filter fails.
*/ */
public static function checkInt($check) public static function checkInt(int $check): bool|int
{ {
return filter_var($check, FILTER_VALIDATE_INT); return filter_var($check, FILTER_VALIDATE_INT);
} }
@ -155,7 +170,7 @@ class SRSSTools
* *
* @return mixed * @return mixed
*/ */
private static function checkMediaType($check) public static function checkMediaType($check): mixed
{ {
return $check; return $check;
} }
@ -165,7 +180,7 @@ class SRSSTools
* *
* @return mixed|null * @return mixed|null
*/ */
private static function checkMediaMedium($check) public static function checkMediaMedium($check): ?string
{ {
return in_array($check, ['image', 'audio', 'video', 'document', 'executable']) ? $check : null; return in_array($check, ['image', 'audio', 'video', 'document', 'executable']) ? $check : null;
} }
@ -175,7 +190,7 @@ class SRSSTools
* *
* @return mixed|null * @return mixed|null
*/ */
private static function checkBool($check) public static function checkBool($check): ?string
{ {
return in_array($check, ['true', 'false']) ? $check : null; return in_array($check, ['true', 'false']) ? $check : null;
} }
@ -185,7 +200,7 @@ class SRSSTools
* *
* @return mixed|null * @return mixed|null
*/ */
private static function checkMediumExpression($check) public static function checkMediumExpression($check): ?string
{ {
return in_array($check, ['sample', 'full', 'nonstop']) ? $check : null; return in_array($check, ['sample', 'full', 'nonstop']) ? $check : null;
} }

View File

@ -15,9 +15,9 @@ class Validator
/** /**
* @throws ReflectionException * @throws ReflectionException
*/ */
public function isPropertyValid($object, $property) public function isPropertyValid($object, $property): bool
{ {
$properties = array_filter($this->_getClassProperties(get_class($object)), fn($p) => $p->getName() === $property); $properties = array_filter($this->_getClassProperties(get_class($object)), static fn($p) => $p->getName() === $property);
if (count($properties) !== 1) { if (count($properties) !== 1) {
return false; return false;
} }
@ -26,7 +26,7 @@ class Validator
$propertyValue = $object->{$properties->name}; $propertyValue = $object->{$properties->name};
$propertyAnnotations = $this->_getPropertyAnnotations($properties); $propertyAnnotations = $this->_getPropertyAnnotations($properties);
if (!in_array('required', $propertyAnnotations, true) && empty($propertyValue)) { if (empty($propertyValue) && !in_array('required', $propertyAnnotations, true)) {
return true; return true;
} }
@ -67,7 +67,7 @@ class Validator
$propertyValue = $object->{$property['name']}; $propertyValue = $object->{$property['name']};
// $propertyAnnotations = $this->_getPropertyAnnotations($property, get_class($object)); // $propertyAnnotations = $this->_getPropertyAnnotations($property, get_class($object));
if (!in_array('required', $property['rules'], true) && empty($propertyValue)) { if (empty($propertyValue) && !in_array('required', $property['rules'], true)) {
continue; continue;
} }
@ -109,7 +109,7 @@ class Validator
{ {
preg_match_all('#@(.*?)\n#s', $property->getDocComment(), $annotations); preg_match_all('#@(.*?)\n#s', $property->getDocComment(), $annotations);
return array_map(fn($annotation) => trim($annotation), $annotations[1]); return array_map(static fn($annotation) => trim($annotation), $annotations[1]);
} }
private function _validateString($value): bool private function _validateString($value): bool
@ -199,7 +199,7 @@ class Validator
); );
} }
private function _validateContentMedia($value) private function _validateContentMedia($value): bool
{ {
if (is_array($value)) { if (is_array($value)) {
foreach ($value as $content) { foreach ($value as $content) {

View File

@ -16,7 +16,7 @@ class BasicBuilderTest extends TestCase
unlink($this->saved); unlink($this->saved);
} }
} }
public function testCreateBasicRSS() public function testCreateBasicRSS(): void
{ {
$srss = SRSS::create(); $srss = SRSS::create();
$srss->title = 'My Blog'; $srss->title = 'My Blog';

View File

@ -9,7 +9,7 @@ use Shikiryu\SRSS\SRSS;
class BasicReaderTest extends TestCase class BasicReaderTest extends TestCase
{ {
public function testReadBasicRSS() public function testReadBasicRSS(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/basic.xml'); $rss = SRSS::read(__DIR__.'/resources/basic.xml');
self::assertEquals('test Home Page', $rss->title); self::assertEquals('test Home Page', $rss->title);
@ -20,7 +20,7 @@ class BasicReaderTest extends TestCase
self::assertTrue($rss->isValid()); self::assertTrue($rss->isValid());
} }
public function testSpecificationExampleRSS() public function testSpecificationExampleRSS(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/harvard.xml'); $rss = SRSS::read(__DIR__.'/resources/harvard.xml');
self::assertEquals('Liftoff News', $rss->title); self::assertEquals('Liftoff News', $rss->title);
@ -41,7 +41,7 @@ class BasicReaderTest extends TestCase
self::assertTrue($rss->isValid()); self::assertTrue($rss->isValid());
} }
public function testChannelImage() public function testChannelImage(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/media/cnn.xml'); $rss = SRSS::read(__DIR__.'/resources/media/cnn.xml');
$image = $rss->image; $image = $rss->image;
@ -51,7 +51,7 @@ class BasicReaderTest extends TestCase
self::assertEquals('https://www.cnn.com/entertainment/index.html', $image->link, var_export($image, true)); self::assertEquals('https://www.cnn.com/entertainment/index.html', $image->link, var_export($image, true));
} }
public function testChannelCategory() public function testChannelCategory(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/cloud.xml'); $rss = SRSS::read(__DIR__.'/resources/cloud.xml');
$categories = $rss->category; $categories = $rss->category;
@ -62,7 +62,7 @@ class BasicReaderTest extends TestCase
self::assertEquals('rssUpdates', $category->value, var_export($category, true)); self::assertEquals('rssUpdates', $category->value, var_export($category, true));
} }
public function testCloud() public function testCloud(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/cloud.xml'); $rss = SRSS::read(__DIR__.'/resources/cloud.xml');
$cloud = $rss->cloud; $cloud = $rss->cloud;
@ -74,7 +74,7 @@ class BasicReaderTest extends TestCase
self::assertEquals('xml-rpc', $cloud->protocol, var_export($cloud, true)); self::assertEquals('xml-rpc', $cloud->protocol, var_export($cloud, true));
} }
public function testSource() public function testSource(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/basic.xml'); $rss = SRSS::read(__DIR__.'/resources/basic.xml');
$firstItem = $rss->getFirst(); $firstItem = $rss->getFirst();
@ -85,7 +85,7 @@ class BasicReaderTest extends TestCase
self::assertEquals('Tomalak\'s Realm', $source->value); self::assertEquals('Tomalak\'s Realm', $source->value);
} }
public function testEnclosure() public function testEnclosure(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/basic.xml'); $rss = SRSS::read(__DIR__.'/resources/basic.xml');
$item = $rss->getItem(2); $item = $rss->getItem(2);

View File

@ -8,22 +8,22 @@ use Shikiryu\SRSS\SRSS;
class ExceptionTest extends TestCase class ExceptionTest extends TestCase
{ {
public function testPropertyNotFound() public function testPropertyNotFound(): void
{ {
$srss = new SRSS(); $srss = new SRSS();
$this->expectException(PropertyNotFoundException::class); $this->expectException(PropertyNotFoundException::class);
$srss->notfound = 'true'; $srss->notfound = 'true';
} }
public function testRssNotFound() public function testRssNotFound(): void
{ {
$this->expectException(UnreadableRSSException::class); $this->expectException(UnreadableRSSException::class);
$rss = SRSS::read('not_found.xml'); SRSS::read('not_found.xml');
} }
public function testMissingChannel() public function testMissingChannel(): void
{ {
$this->expectException(ChannelNotFoundInRSSException::class); $this->expectException(ChannelNotFoundInRSSException::class);
$rss = SRSS::read(__DIR__ . '/resources/invalid-no-channel.xml'); SRSS::read(__DIR__ . '/resources/invalid-no-channel.xml');
} }
} }

View File

@ -5,7 +5,7 @@ use Shikiryu\SRSS\SRSS;
class MediaTest extends TestCase class MediaTest extends TestCase
{ {
public function testImages() public function testImages(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/media/cnn.xml'); $rss = SRSS::read(__DIR__.'/resources/media/cnn.xml');
self::assertEquals('CNN.com - RSS Channel - Entertainment', $rss->title); self::assertEquals('CNN.com - RSS Channel - Entertainment', $rss->title);
@ -17,7 +17,7 @@ class MediaTest extends TestCase
self::assertTrue($rss->isValid(), var_export($rss->channel->validated, true)); self::assertTrue($rss->isValid(), var_export($rss->channel->validated, true));
} }
public function testMusicVideo() public function testMusicVideo(): void
{ {
$rss = SRSS::read(__DIR__.'/resources/media/music-video.xml'); $rss = SRSS::read(__DIR__.'/resources/media/music-video.xml');
self::assertEquals('Music Videos 101', $rss->title); self::assertEquals('Music Videos 101', $rss->title);

View File

@ -16,7 +16,7 @@ class OriginalReaderSRSSTest extends TestCase
} }
} }
public function testOriginalReader() public function testOriginalReader(): void
{ {
$rss = SRSS::read($this->original); $rss = SRSS::read($this->original);
self::assertEquals('Liftoff News', $rss->title); self::assertEquals('Liftoff News', $rss->title);

View File

@ -7,7 +7,7 @@ use Shikiryu\SRSS\SRSSTools;
class OriginalWriterSRSSTest extends TestCase class OriginalWriterSRSSTest extends TestCase
{ {
public function testOriginalWriter() public function testOriginalWriter(): void
{ {
$rss = SRSS::create(); $rss = SRSS::create();
$rss->title = 'My Awesome Blog'; $rss->title = 'My Awesome Blog';
@ -22,12 +22,12 @@ class OriginalWriterSRSSTest extends TestCase
]; ];
foreach($items as $item){ foreach($items as $item){
$rssitem = new Item(); $rss_item = new Item();
$rssitem->title = $item["title"]; $rss_item->title = $item["title"];
$rssitem->link = $item['link']; $rss_item->link = $item['link'];
$rssitem->pubDate = $item["pubDate"]; $rss_item->pubDate = $item["pubDate"];
$rssitem->description = $item["description"]; $rss_item->description = $item["description"];
$rss->addItem($rssitem); $rss->addItem($rss_item);
} }
$firstItem = new Item(); $firstItem = new Item();