Add more type checking to satisfy PHPStan and update some PHPStan exceptions

This commit is contained in:
Alex Cabal 2020-06-26 12:52:44 -05:00
parent 43b9f1a6f5
commit 5b2557c858
3 changed files with 37 additions and 7 deletions

View file

@ -13,6 +13,12 @@ parameters:
# Ignore errors caused by missing phpdoc strings for arrays # Ignore errors caused by missing phpdoc strings for arrays
- '#Method .+? has parameter .+? with no value type specified in iterable type array.#' - '#Method .+? has parameter .+? with no value type specified in iterable type array.#'
# Ignore errors caused by type hints that should be union types. Union types are not yet supported in PHP.
- '#Function vd(s|d)?\(\) has parameter \$var with no typehint specified.#'
- '#Method Ebook::NullIfEmpty\(\) has parameter \$elements with no typehint specified.#'
- '#Method HttpInput::GetHttpVar\(\) has no return typehint specified.#'
- '#Method HttpInput::GetHttpVar\(\) has parameter \$default with no typehint specified.#'
level: level:
7 7
paths: paths:

View file

@ -1,6 +1,7 @@
<? <?
use function Safe\file_get_contents; use function Safe\file_get_contents;
use function Safe\file_put_contents; use function Safe\file_put_contents;
use function Safe\preg_replace;
use function Safe\rename; use function Safe\rename;
use function Safe\tempnam; use function Safe\tempnam;
@ -22,14 +23,24 @@ class OpdsFeed{
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xmlString)); $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xmlString));
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); $xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$xml->registerXPathNamespace('schema', 'http://schema.org/'); $xml->registerXPathNamespace('schema', 'http://schema.org/');
$entries = $xml->xpath('/feed/entry') ?? []; $entries = $xml->xpath('/feed/entry');
if($entries === false){
$entries = [];
}
$output = ''; $output = '';
foreach($entries as $entry){ foreach($entries as $entry){
// Remove any <updated> elements, we don't want to compare against those. // Remove any <updated> elements, we don't want to compare against those.
// This makes it easier to for example generate a new subjects index, // This makes it easier to for example generate a new subjects index,
// while updating it at the same time. // while updating it at the same time.
foreach($xml->xpath('/feed/entry/updated') as $element){ $elements = $xml->xpath('/feed/entry/updated');
if($elements === false){
$elements = [];
}
foreach($elements as $element){
unset($element[0]); unset($element[0]);
} }
@ -58,9 +69,21 @@ class OpdsFeed{
} }
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($parentFilepath))); $xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', file_get_contents($parentFilepath)));
$feedEntry = ($xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated') ?? [])[0]; $feedEntries = $xml->xpath('/feed/entry[id="' . $this->Id . '"]/updated');
$feedEntry[0] = $updatedTimestamp; if($feedEntries === false){
file_put_contents($parentFilepath, str_replace(" ns=", " xmlns=", $xml->asXml() ?? '')); $feedEntries = [];
}
if(sizeof($feedEntries) > 0){
$feedEntries[0][0] = $updatedTimestamp;
}
$xmlString = $xml->asXml();
if($xmlString === false){
$xmlString = '';
}
file_put_contents($parentFilepath, str_replace(" ns=", " xmlns=", $xmlString));
rename($tempFilename, $path); rename($tempFilename, $path);
} }

View file

@ -4,6 +4,7 @@ require_once('/standardebooks.org/web/lib/Core.php');
use function Safe\krsort; use function Safe\krsort;
use function Safe\getopt; use function Safe\getopt;
use function Safe\preg_replace; use function Safe\preg_replace;
use function Safe\sort;
$longopts = array("webroot:", "weburl:"); $longopts = array("webroot:", "weburl:");
$options = getopt("", $longopts); $options = getopt("", $longopts);
@ -51,8 +52,8 @@ $subjectsFeed->Save(WEB_ROOT . '/opds/subjects/index.xml');
// Now generate each individual subject feed // Now generate each individual subject feed
foreach($ebooksBySubject as $subject => $ebooks){ foreach($ebooksBySubject as $subject => $ebooks){
krsort($ebooks); krsort($ebooks);
$subjectFeed = new OpdsAcquisitionFeed('/opds/subjects/' . Formatter::MakeUrlSafe($subject), $subject, '/opds/subjects', $ebooks); $subjectFeed = new OpdsAcquisitionFeed('/opds/subjects/' . Formatter::MakeUrlSafe((string)$subject), (string)$subject, '/opds/subjects', $ebooks);
$subjectFeed->Save(WEB_ROOT . '/opds/subjects/' . Formatter::MakeUrlSafe($subject) . '.xml'); $subjectFeed->Save(WEB_ROOT . '/opds/subjects/' . Formatter::MakeUrlSafe((string)$subject) . '.xml');
} }
// Create the 'all' feed // Create the 'all' feed