squash! Use JSLikeHTMLElement in type hints

We cannot use stubs since PHPStan already has them to override
pull/93/head
Jan Tojnar 1 year ago
parent 2e9d766f82
commit 46ec78839d
  1. 5
      composer.json
  2. 38
      maintenance/PHPStan/DOMDocumentGetElementDynamicReturnTypeExtension.php
  3. 39
      maintenance/PHPStan/DOMDocumentGetElementsDynamicReturnTypeExtension.php
  4. 39
      maintenance/PHPStan/DOMElementGetElementsDynamicReturnTypeExtension.php
  5. 20
      phpstan.dist.neon
  6. 2
      src/Readability.php
  7. 40
      stubs/dom.stub

@ -44,7 +44,10 @@
"psr-4": { "Readability\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Tests\\Readability\\": "tests/" }
"psr-4": {
"Tests\\Readability\\": "tests/",
"Readability\\Maintenance\\": "maintenance/"
}
},
"scripts": {
"fix": "php-cs-fixer fix --verbose --diff",

@ -0,0 +1,38 @@
<?php
namespace Readability\Maintenance\PHPStan;
use Readability\JSLikeHTMLElement;
use PHPStan\Type\Generic\GenericObjectType;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
class DOMDocumentGetElementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return \DOMDocument::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(),
[
'createElement',
], true);
}
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): ?Type
{
return new ObjectType(JSLikeHTMLElement::class);
}
}

@ -0,0 +1,39 @@
<?php
namespace Readability\Maintenance\PHPStan;
use Readability\JSLikeHTMLElement;
use PHPStan\Type\Generic\GenericObjectType;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
class DOMDocumentGetElementsDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return \DOMDocument::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(),
[
'getElementsByTagName',
], true);
}
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): ?Type
{
$elementType = new ObjectType(JSLikeHTMLElement::class);
return new GenericObjectType(\DOMNodeList::class, [$elementType]);
}
}

@ -0,0 +1,39 @@
<?php
namespace Readability\Maintenance\PHPStan;
use Readability\JSLikeHTMLElement;
use PHPStan\Type\Generic\GenericObjectType;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
class DOMElementGetElementsDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return \DOMElement::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(),
[
'getElementsByTagName',
], true);
}
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): ?Type
{
$elementType = new ObjectType(JSLikeHTMLElement::class);
return new GenericObjectType(\DOMNodeList::class, [$elementType]);
}
}

@ -8,9 +8,23 @@ parameters:
bootstrapFiles:
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
stubFiles:
- stubs/dom.stub
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- phar://phpstan.phar/conf/bleedingEdge.neon
services:
-
class: Readability\Maintenance\PHPStan\DOMDocumentGetElementDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Readability\Maintenance\PHPStan\DOMDocumentGetElementsDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Readability\Maintenance\PHPStan\DOMElementGetElementsDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

@ -844,7 +844,7 @@ class Readability implements LoggerAwareInterface
/**
* Get the article title as an H1.
*
* @return \DOMElement
* @return JSLikeHTMLElement
*/
protected function getArticleTitle()
{

@ -1,40 +0,0 @@
<?php
// SPDX-FileCopyrightText: 2022 Ondřej Mirtes
// SPDX-License-Identifier: MIT
// Based on https://github.com/phpstan/phpstan-src/blob/b2a9ba4b82d19b01f37eb983746f1840f1213851/stubs/dom.stub
use Readability\JSLikeHTMLElement;
class DOMDocument
{
/** @var JSLikeHTMLElement|null */
public $documentElement;
/**
* @param string $name
* @return DOMNodeList<JSLikeHTMLElement>
*/
public function getElementsByTagName($name) {}
/**
* @param string $localName
* @param string $value
* @return JSLikeHTMLElement Officially, this can return false but PHPStan decided to ignore that: <https://github.com/phpstan/phpstan-src/pull/1569>
*/
public function createElement($localName, $value = '') {}
}
class DOMNode
{
}
class DOMElement extends DOMNode
{
/**
* @param string $name
* @return DOMNodeList<JSLikeHTMLElement>
*/
public function getElementsByTagName($name) {}
}
Loading…
Cancel
Save