Browse Source

Various updates

- Fix deprecation notices in PHP 8.x
- Use DOMParser to parse HTML and XML documents
- Update Robo, php-cs-fixer
- Clean up tooling where practical
master
J. King 1 year ago
parent
commit
d50c7a527d
  1. 1
      .gitignore
  2. 29
      .php-cs-fixer.dist.php
  3. 42
      RoboFile.php
  4. 1
      composer.json
  5. 127
      composer.lock
  6. 4
      lib/Collection.php
  7. 3
      lib/Date.php
  8. 1
      lib/Enclosure/Enclosure.php
  9. 3
      lib/Parser/AbstractEntry.php
  10. 2
      lib/Parser/AbstractFeed.php
  11. 13
      lib/Parser/HTML/Feed.php
  12. 7
      lib/Parser/XML/Feed.php
  13. 1
      lib/Parser/XML/XPath.php
  14. 2
      vendor-bin/csfixer/composer.json
  15. 801
      vendor-bin/csfixer/composer.lock
  16. 2
      vendor-bin/phpunit/composer.json
  17. 580
      vendor-bin/phpunit/composer.lock
  18. 2
      vendor-bin/robo/composer.json
  19. 925
      vendor-bin/robo/composer.lock

1
.gitignore

@ -3,3 +3,4 @@
/tests/coverage
/tests/.phpunit.result.cache
/.php_cs.cache
/.php-cs-fixer.cache

29
.php_cs.dist → .php-cs-fixer.dist.php

@ -1,4 +1,7 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
@ -19,6 +22,7 @@ $rules = [
'operators' => ['=>' => "align_single_space"],
],
'cast_spaces' => ['space' => "single"],
'concat_space' => ['spacing' => "none"],
'list_syntax' => ['syntax' => "short"],
'magic_constant_casing' => true,
'magic_method_casing' => true,
@ -29,6 +33,7 @@ $rules = [
'no_blank_lines_after_phpdoc' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true, // this could probably use more configuration
'no_mixed_echo_print' => ['use' => "echo"],
'no_short_bool_cast' => true,
@ -42,32 +47,14 @@ $rules = [
'pow_to_exponentiation' => true,
'set_type_to_cast' => true,
'standardize_not_equals' => true,
'trailing_comma_in_multiline_array' => true,
'trailing_comma_in_multiline' => ['elements' => ["arrays"]],
'unary_operator_spaces' => true,
'yoda_style' => false,
// PSR standard to apply
'@PSR2' => true,
// PSR-12 rules; php-cs-fixer does not yet support PSR-12 natively
'blank_line_after_opening_tag' => true,
'compact_nullable_typehint' => true,
'declare_equal_normalize' => ['space' => "none"],
'function_typehint_space' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_alternative_syntax' => true,
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_whitespace_in_blank_line' => true,
'return_type_declaration' => ['space_before' => "none"],
'single_trait_insert_per_statement' => true,
'short_scalar_cast' => true,
'visibility_required' => ['elements' => ["const", "property", "method"]],
'@PSR12' => true,
// house exceptions to PSR rules
'braces' => ['position_after_functions_and_oop_constructs' => "same"],
'function_declaration' => ['closure_function_spacing' => "none"],
'concat_space' => ['spacing' => "none"],
'new_with_braces' => false, // no option to specify absence of braces
];
@ -79,4 +66,4 @@ foreach ($paths as $path) {
$finder = $finder->in($path);
}
}
return \PhpCsFixer\Config::create()->setRiskyAllowed(true)->setRules($rules)->setFinder($finder);
return (new \PhpCsFixer\Config)->setRiskyAllowed(true)->setRules($rules)->setFinder($finder);

42
RoboFile.php

@ -6,6 +6,8 @@ const BASE = __DIR__.\DIRECTORY_SEPARATOR;
const BASE_TEST = BASE."tests".\DIRECTORY_SEPARATOR;
define("IS_WIN", defined("PHP_WINDOWS_VERSION_MAJOR"));
define("IS_MAC", php_uname("s") === "Darwin");
define("IS_LINUX", !IS_WIN && !IS_MAC);
error_reporting(0);
function norm(string $path): string {
$out = realpath($path);
@ -53,8 +55,8 @@ class RoboFile extends \Robo\Tasks {
* tests/coverage/. Additional reports may be produced by passing
* arguments to this task as one would to PHPUnit.
*
* Robo first tries to use pcov and will fall back first to xdebug then
* phpdbg. Neither pcov nor xdebug need to be enabled to be used; they
* Robo first tries to use pcov and will fall back to xdebug.
* Neither pcov nor xdebug need to be enabled to be used; they
* only need to be present in the extension load path to be used.
*/
public function coverage(array $args): Result {
@ -78,7 +80,7 @@ class RoboFile extends \Robo\Tasks {
return $this->runTests($exec, "typical", array_merge(["--coverage-html", BASE_TEST."coverage"], $args));
}
/** Runs the coding standards fixer */
/** Runs the coding-style fixer */
public function clean($opts = ['demo|d' => false]): Result {
$t = $this->taskExec(norm(BASE."vendor/bin/php-cs-fixer"));
$t->arg("fix");
@ -88,39 +90,42 @@ class RoboFile extends \Robo\Tasks {
return $t->run();
}
/** Finds the first suitable means of computing code coverage, either pcov or xdebug. */
protected function findCoverageEngine(): string {
$dir = rtrim(ini_get("extension_dir"), "/").\DIRECTORY_SEPARATOR;
$ext = IS_WIN ? "dll" : (IS_MAC ? "dylib" : "so");
$ext = IS_WIN ? "dll" : "so";
$php = escapeshellarg(\PHP_BINARY);
$code = escapeshellarg(BASE."lib");
if (extension_loaded("pcov")) {
return "$php -d pcov.enabled=1 -d pcov.directory=$code";
} elseif (extension_loaded("xdebug")) {
return $php;
return "$php -d xdebug.mode=coverage";
} elseif (file_exists($dir."pcov.$ext")) {
return "$php -d extension=pcov.$ext -d pcov.enabled=1 -d pcov.directory=$code";
} elseif (file_exists($dir."pcov.$ext")) {
return "$php -d zend_extension=xdebug.$ext";
} elseif (file_exists($dir."xdebug.$ext")) {
return "$php -d zend_extension=xdebug.$ext -d xdebug.mode=coverage";
} else {
if (IS_WIN) {
$dbg = dirname(\PHP_BINARY)."\\phpdbg.exe";
$dbg = file_exists($dbg) ? $dbg : "";
} else {
$dbg = trim(`which phpdbg 2>/dev/null`);
}
if ($dbg) {
return escapeshellarg($dbg)." -qrr";
} else {
return $php;
}
return $php;
}
}
/** Returns the necessary shell arguments to print error output or all output to the bitbucket
*
* @param bool $all Whether all output (true) or only error output (false) should be suppressed
*/
protected function blackhole(bool $all = false): string {
$hole = IS_WIN ? "nul" : "/dev/null";
return $all ? ">$hole 2>&1" : "2>$hole";
}
/** Executes PHPUnit, used by the test and coverage tasks.
*
* This also executes the built-in PHP Web server, which is required to fetch some newsfeeds during tests
*
* @param string $executor The path to the PHP binary to execute with any required extra arguments. Normally this is either "php" or the result of findCoverageEngine()
* @param string $set The set of tests to run, either "typical" (excludes redundant tests), "quick" (excludes redundant and slow tests), "coverage" (excludes tests not needed for coverage), or "full" (all tests)
* @param array $args Extra arguments passed by Robo from the command line
*/
protected function runTests(string $executor, string $set, array $args): Result {
switch ($set) {
case "typical":
@ -140,7 +145,6 @@ class RoboFile extends \Robo\Tasks {
}
$execpath = norm(BASE."vendor-bin/phpunit/vendor/phpunit/phpunit/phpunit");
$confpath = realpath(BASE_TEST."phpunit.dist.xml") ?: norm(BASE_TEST."phpunit.xml");
//$this->taskServer(8000)->host("localhost")->dir(BASE_TEST."docroot")->rawArg("-n")->arg(BASE_TEST."server.php")->rawArg($this->blackhole())->background()->run();
return $this->taskExec($executor)->option("-d", "zend.assertions=1")->arg($execpath)->option("-c", $confpath)->args(array_merge($set, $args))->run();
}
}

1
composer.json

@ -20,6 +20,7 @@
"ralouphie/mimey": "^2.1",
"psr/http-message": "^1.0",
"mensbeam/mimesniff": "^0.2.0",
"mensbeam/html-parser": "^1.3.0",
"psr/http-factory": "^1.0",
"psr/http-client": "^1.0"
},

127
composer.lock

@ -4,8 +4,133 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2d0bef53f4eaeeba37004efdea63426e",
"content-hash": "18bcc0b76759cef8c7c71e72156431bf",
"packages": [
{
"name": "mensbeam/html-parser",
"version": "1.3.0",
"source": {
"type": "git",
"url": "https://github.com/mensbeam/HTML-Parser.git",
"reference": "d499fac607ae06311df54887ba45c120797c76ed"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mensbeam/HTML-Parser/zipball/d499fac607ae06311df54887ba45c120797c76ed",
"reference": "d499fac607ae06311df54887ba45c120797c76ed",
"shasum": ""
},
"require": {
"ext-dom": "*",
"mensbeam/intl": ">=0.9.1",
"mensbeam/mimesniff": ">=0.2.0",
"php": ">=7.1"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.3"
},
"suggest": {
"ext-ctype": "Improved performance"
},
"type": "library",
"autoload": {
"files": [
"lib/Parser/ctype.php"
],
"psr-4": {
"MensBeam\\HTML\\": [
"lib/"
]
},
"classmap": [
"lib/Parser/Token.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Dustin Wilson",
"email": "dustin@dustinwilson.com",
"homepage": "https://dustinwilson.com/"
},
{
"name": "J. King",
"email": "jking@jkingweb.ca",
"homepage": "https://jkingweb.ca/"
}
],
"description": "Parser and serializer for modern HTML documents",
"keywords": [
"HTML5",
"WHATWG",
"dom",
"html",
"parser",
"parsing"
],
"support": {
"source": "https://github.com/mensbeam/HTML-Parser/tree/1.3.0"
},
"time": "2023-04-02T01:38:10+00:00"
},
{
"name": "mensbeam/intl",
"version": "0.9.2",
"source": {
"type": "git",
"url": "https://github.com/mensbeam/intl.git",
"reference": "88dbf8398ab69e71164ac073f9ec011be2baa4ae"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mensbeam/intl/zipball/88dbf8398ab69e71164ac073f9ec011be2baa4ae",
"reference": "88dbf8398ab69e71164ac073f9ec011be2baa4ae",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"require-dev": {
"bamarni/composer-bin-plugin": "*",
"ext-intl": "*"
},
"type": "library",
"autoload": {
"psr-4": {
"MensBeam\\Intl\\": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "J. King",
"email": "jking@jkingweb.ca",
"homepage": "https://jkingweb.ca/"
}
],
"description": "A set of dependency-free basic internationalization tools",
"keywords": [
"WHATWG",
"charset",
"encoding",
"internationalization",
"intl",
"unicode",
"utf-8",
"utf8"
],
"support": {
"issues": "https://github.com/mensbeam/intl/issues",
"source": "https://github.com/mensbeam/intl/tree/0.9.2"
},
"time": "2023-01-25T22:12:58+00:00"
},
{
"name": "mensbeam/mimesniff",
"version": "0.2.1",

4
lib/Collection.php

@ -15,6 +15,7 @@ abstract class Collection implements \IteratorAggregate, \ArrayAccess, \Countabl
}
/** Implementation for JsonSerializable */
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return $this->data;
}
@ -30,11 +31,13 @@ abstract class Collection implements \IteratorAggregate, \ArrayAccess, \Countabl
}
/** Implementation for ArrayAccess */
#[\ReturnTypeWillChange]
public function offsetGet($offset) {
return $this->data[$offset];
}
/** Implementation for ArrayAccess */
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->data[] = $value;
@ -44,6 +47,7 @@ abstract class Collection implements \IteratorAggregate, \ArrayAccess, \Countabl
}
/** Implementation for ArrayAccess */
#[\ReturnTypeWillChange]
public function offsetUnset($offset) {
unset($this->data[$offset]);
}

3
lib/Date.php

@ -174,11 +174,13 @@ class Date extends \DateTimeImmutable implements \JsonSerializable {
return $time;
}
#[\ReturnTypeWillChange]
public static function createFromFormat($format, $time, $timezone = null): ?self {
$temp = parent::createFromFormat("!".$format, $time, $timezone);
return $temp ? static::create($temp) : null;
}
#[\ReturnTypeWillChange]
public static function createFromMutable($datetime): ?self {
$temp = parent::createFromMutable($datetime);
return $temp ? static::create($temp) : null;
@ -202,6 +204,7 @@ class Date extends \DateTimeImmutable implements \JsonSerializable {
}
}
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return $this->__toString();
}

1
lib/Enclosure/Enclosure.php

@ -47,6 +47,7 @@ class Enclosure implements \IteratorAggregate, \ArrayAccess, \Countable {
return isset($this->data[$offset]);
}
#[\ReturnTypeWillChange]
public function offsetGet($offset) {
return $this->data[$offset];
}

3
lib/Parser/AbstractEntry.php

@ -10,6 +10,9 @@ use MensBeam\Lax\Feed as FeedStruct;
use MensBeam\Lax\Entry as EntryStruct;
trait AbstractEntry {
protected $data;
protected $feed;
public function __construct($data, FeedStruct $feed) {
$this->data = $data;
$this->feed = $feed;

2
lib/Parser/AbstractFeed.php

@ -10,6 +10,8 @@ use MensBeam\Lax\Url;
use MensBeam\Lax\Feed as FeedStruct;
trait AbstractFeed {
protected $version;
/** Constructs a feed parser */
public function __construct(string $data, string $contentType = null, string $url = null) {
$this->data = $data;

13
lib/Parser/HTML/Feed.php

@ -16,6 +16,7 @@ use MensBeam\Lax\Parser\Exception;
use MensBeam\Lax\Parser\XML\XPath;
use MensBeam\Lax\Category\Collection as CategoryCollection;
use MensBeam\Lax\Person\Collection as PersonCollection;
use MensBeam\HTML\DOMParser;
class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
use \MensBeam\Lax\Parser\AbstractFeed;
@ -39,13 +40,14 @@ class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
/** Performs initialization of the instance */
protected function init(FeedStruct $feed): FeedStruct {
$type = MimeType::parse($this->contentType) ?? "";
$type = MimeType::parse($this->contentType);
if ($type && !in_array($type->essence, self::MIME_TYPES)) {
throw new Exception("notHTMLType");
}
$parser = new DOMParser;
if ($type && $type->essence === "application/xhtml+xml") {
$this->document = new \DOMDocument;
if (!$this->document->loadXML($this->data, self::LIBXML_OPTIONS)) {
$this->document = $parser->parseFromString($this->data, $this->contentType);
if ($this->document->documentElement->tagName === "parsererror" && $this->document->documentElement->namespaceURI === "http://www.mozilla.org/newlayout/xml/parsererror.xml") {
// ignore XML parsing errors; we will reparse as HTML in this case
$this->document = null;
} elseif ($this->document->documentElement->namespaceURI !== XPath::NS['html']) {
@ -53,10 +55,7 @@ class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
}
}
if (!$this->document) {
$this->document = new \DOMDocument;
if (!$this->document->loadHTML($this->data, self::LIBXML_OPTIONS)) {
throw new Exception("notHTML"); // @codeCoverageIgnore
}
$this->document = $parser->parseFromString($this->data, "text/html;charset=".($type->params['charset'] ?? ""));
}
$this->document->documentURI = $this->url;
$this->xpath = new \DOMXPath($this->document);

7
lib/Parser/XML/Feed.php

@ -17,6 +17,7 @@ use MensBeam\Lax\MimeType;
use MensBeam\Lax\Schedule;
use MensBeam\Lax\Text;
use MensBeam\Lax\Url;
use MensBeam\HTML\DOMParser;
class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
use \MensBeam\Lax\Parser\AbstractFeed;
@ -43,12 +44,12 @@ class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
/** Performs initialization of the instance */
protected function init(FeedStruct $feed): FeedStruct {
$type = MimeType::parse($this->contentType) ?? "";
$type = MimeType::parse($this->contentType) ?? null;
if ($type && !in_array($type->essence, self::MIME_TYPES)) {
throw new Exception("notXMLType");
}
$this->document = new \DOMDocument();
if (!$this->document->loadXML($this->data, self::LIBXML_OPTIONS)) {
$this->document = (new DOMParser)->parseFromString($this->data, $type ?? "text/xml");
if ($this->document->documentElement->tagName === "parsererror" && $this->document->documentElement->namespaceURI === "http://www.mozilla.org/newlayout/xml/parsererror.xml") {
throw new Exception("notXML");
}
$this->document->documentURI = $this->url;

1
lib/Parser/XML/XPath.php

@ -36,6 +36,7 @@ class XPath extends \DOMXpath {
}
/** {@inheritDoc} */
#[\ReturnTypeWillChange]
public function query($expression, $contextnode = null, $registerNS = true) {
$expression = $this->rss2 ? str_replace("rss2:", "", $expression) : $expression;
$expression = $this->html ? str_replace("html:", "", $expression) : $expression;

2
vendor-bin/csfixer/composer.json

@ -1,5 +1,5 @@
{
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16"
"friendsofphp/php-cs-fixer": "^3.0"
}
}

801
vendor-bin/csfixer/composer.lock

File diff suppressed because it is too large

2
vendor-bin/phpunit/composer.json

@ -4,6 +4,6 @@
"symfony/yaml": "^5.0",
"guzzlehttp/guzzle": "^6.5",
"guzzlehttp/psr7": "^1.6",
"phake/phake": "^3.1"
"phake/phake": "^4.4"
}
}

580
vendor-bin/phpunit/composer.lock

File diff suppressed because it is too large

2
vendor-bin/robo/composer.json

@ -1,5 +1,5 @@
{
"require-dev": {
"consolidation/robo": "^2.0"
"consolidation/robo": "^4.0"
}
}

925
vendor-bin/robo/composer.lock

File diff suppressed because it is too large
Loading…
Cancel
Save