Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase type safety #87

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft

Increase type safety #87

wants to merge 12 commits into from

Conversation

jtojnar
Copy link
Contributor

@jtojnar jtojnar commented Mar 16, 2024

This will allow us to use flexible heredocs in test, as well as typed properties and other goodies.

https://www.php.net/releases/7_3_0.php
https://www.php.net/releases/7_4_0.php

@jtojnar jtojnar marked this pull request as draft March 16, 2024 15:43
@jtojnar jtojnar force-pushed the more-stricter branch 2 times, most recently from af19fe7 to b23a226 Compare March 16, 2024 16:46
@jtojnar jtojnar changed the title Drop PHP < 7.4 support Increase type safety Mar 16, 2024
This was referenced Mar 16, 2024
Breaking BC for Readability 3.0.
Not sure if this is expected but at least it works the same as before.
`DOMAttr::$value` must be a `string`.

Let’s add helpers for manipulating the `readability` attribute
so that we do not have to keep casting it from and to `string`
in order to appease `strict_types`.
PHP 7.4 supports property type hints.

- `getInnerText` actually accepts `DOMNode`, not `DOMElement`, since it can be e.g. `DOMText`.
  Really, we just need the `textContent` property.
- Since we now use property type hints,
  we can drop the `no_null_property_initialization` PHP-CS-Fixer rule.
It is more specific then DOMElement.

This allows us to get rid of the assertions in tests.
Will make it easier to statically analyze with PHPStan.

This is a BC break.
Nothing affecting correctness, just stuff making it easier for PHPStan to reason about the code.

Remove `$errcontext` argument in `set_error_handler` since it is removed in PHP 8.
It would fail for e.g. `<div> <p>foo</p> </div>`.

mozilla/readability uses children for the tag lookup, which return only elements.
PHP does not have children property so b580cf2
mistakenly used `childNodes` instead, but that can return any node type.

Let’s filter the children ourselves.

Also add comments from mozilla/readability’s `_hasSingleTagInsideElement`.
Converting `hasSingleTagInsideElement` into a type-safe getter
will allow PHPStan to know the type of `newNode` is `DOMElement`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant