Skip to content

Commit 8af69ad

Browse files
authored
Merge pull request #71 from j0k3r/feature/enable-rector
Add Rector
2 parents ccf1b33 + c2a1639 commit 8af69ad

File tree

5 files changed

+104
-68
lines changed

5 files changed

+104
-68
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
/.php_cs export-ignore
77
/phpunit.xml.dist export-ignore
88
/phpstan.neon export-ignore
9+
/rector.php export-ignore
910
/.github export-ignore
1011
/tests export-ignore

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"monolog/monolog": "^1.24|^2.1",
3535
"symfony/phpunit-bridge": "^4.4|^5.3|^6.0",
3636
"phpstan/phpstan": "^1.3",
37-
"phpstan/phpstan-phpunit": "^1.0"
37+
"phpstan/phpstan-phpunit": "^1.0",
38+
"rector/rector": "^0.12.15"
3839
},
3940
"suggest": {
4041
"ext-tidy": "Used to clean up given HTML and to avoid problems with bad HTML structure."

rector.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Core\Configuration\Option;
6+
use Rector\Core\ValueObject\PhpVersion;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
9+
10+
return static function (ContainerConfigurator $containerConfigurator): void {
11+
$parameters = $containerConfigurator->parameters();
12+
13+
// paths to refactor; solid alternative to CLI arguments
14+
$parameters->set(Option::PATHS, [
15+
__DIR__ . '/src',
16+
__DIR__ . '/tests',
17+
]);
18+
19+
// Path to phpstan with extensions, that PHPSTan in Rector uses to determine types
20+
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan.neon');
21+
22+
$parameters->set(Option::BOOTSTRAP_FILES, [
23+
__DIR__ . '/vendor/bin/.phpunit/phpunit-8.5-0/vendor/autoload.php',
24+
__DIR__ . '/vendor/autoload.php',
25+
]);
26+
27+
// Define what rule sets will be applied
28+
$containerConfigurator->import(LevelSetList::UP_TO_PHP_72);
29+
30+
// is your PHP version different from the one your refactor to?
31+
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_72);
32+
};

src/Readability.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Readability;
44

5+
use DOMElement;
56
use Masterminds\HTML5;
67
use Psr\Log\LoggerAwareInterface;
78
use Psr\Log\LoggerInterface;
@@ -164,7 +165,7 @@ public function setLogger(LoggerInterface $logger): void
164165
/**
165166
* Get article title element.
166167
*
167-
* @return \DOMElement
168+
* @return DOMElement
168169
*/
169170
public function getTitle()
170171
{
@@ -174,7 +175,7 @@ public function getTitle()
174175
/**
175176
* Get article content element.
176177
*
177-
* @return \DOMElement
178+
* @return DOMElement
178179
*/
179180
public function getContent()
180181
{
@@ -283,7 +284,7 @@ public function init(): bool
283284
/**
284285
* Run any post-process modifications to article content as necessary.
285286
*/
286-
public function postProcessContent(\DOMElement $articleContent): void
287+
public function postProcessContent(DOMElement $articleContent): void
287288
{
288289
if ($this->convertLinksToFootnotes && !preg_match('/\bwiki/', $this->url)) {
289290
$this->addFootnotes($articleContent);
@@ -295,7 +296,7 @@ public function postProcessContent(\DOMElement $articleContent): void
295296
*
296297
* @see http://www.roughtype.com/archives/2010/05/experiments_in.php
297298
*/
298-
public function addFootnotes(\DOMElement $articleContent): void
299+
public function addFootnotes(DOMElement $articleContent): void
299300
{
300301
$footnotesWrapper = $this->dom->createElement('footer');
301302
$footnotesWrapper->setAttribute('class', 'readability-footnotes');
@@ -359,7 +360,7 @@ public function addFootnotes(\DOMElement $articleContent): void
359360
*/
360361
public function prepArticle(\DOMNode $articleContent): void
361362
{
362-
if (!$articleContent instanceof \DOMElement) {
363+
if (!$articleContent instanceof DOMElement) {
363364
return;
364365
}
365366

@@ -456,9 +457,9 @@ public function prepArticle(\DOMNode $articleContent): void
456457
* Get the inner text of a node.
457458
* This also strips out any excess whitespace to be found.
458459
*
459-
* @param \DOMElement $e
460-
* @param bool $normalizeSpaces (default: true)
461-
* @param bool $flattenLines (default: false)
460+
* @param DOMElement $e
461+
* @param bool $normalizeSpaces (default: true)
462+
* @param bool $flattenLines (default: false)
462463
*/
463464
public function getInnerText($e, bool $normalizeSpaces = true, bool $flattenLines = false): string
464465
{
@@ -482,7 +483,7 @@ public function getInnerText($e, bool $normalizeSpaces = true, bool $flattenLine
482483
/**
483484
* Remove the style attribute on every $e and under.
484485
*/
485-
public function cleanStyles(\DOMElement $e): void
486+
public function cleanStyles(DOMElement $e): void
486487
{
487488
if (\is_object($e)) {
488489
$elems = $e->getElementsByTagName('*');
@@ -515,7 +516,7 @@ public function getWordCount(string $text): int
515516
* This is the amount of text that is inside a link divided by the total text in the node.
516517
* Can exclude external references to differentiate between simple text and menus/infoblocks.
517518
*/
518-
public function getLinkDensity(\DOMElement $e, bool $excludeExternal = false): float
519+
public function getLinkDensity(DOMElement $e, bool $excludeExternal = false): float
519520
{
520521
$links = $e->getElementsByTagName('a');
521522
$textLength = mb_strlen($this->getInnerText($e, true, true));
@@ -538,7 +539,7 @@ public function getLinkDensity(\DOMElement $e, bool $excludeExternal = false): f
538539
/**
539540
* Get an element relative weight.
540541
*/
541-
public function getWeight(\DOMElement $e): int
542+
public function getWeight(DOMElement $e): int
542543
{
543544
if (!$this->flagIsActive(self::FLAG_WEIGHT_ATTRIBUTES)) {
544545
return 0;
@@ -556,7 +557,7 @@ public function getWeight(\DOMElement $e): int
556557
/**
557558
* Remove extraneous break tags from a node.
558559
*/
559-
public function killBreaks(\DOMElement $node): void
560+
public function killBreaks(DOMElement $node): void
560561
{
561562
$html = $node->getInnerHTML();
562563
$html = preg_replace($this->regexps['killBreaks'], '<br />', $html);
@@ -569,7 +570,7 @@ public function killBreaks(\DOMElement $node): void
569570
*
570571
* Updated 2012-09-18 to preserve youtube/vimeo iframes
571572
*/
572-
public function clean(\DOMElement $e, string $tag): void
573+
public function clean(DOMElement $e, string $tag): void
573574
{
574575
$targetList = $e->getElementsByTagName($tag);
575576
$isEmbed = ('audio' === $tag || 'video' === $tag || 'iframe' === $tag || 'object' === $tag || 'embed' === $tag);
@@ -601,7 +602,7 @@ public function clean(\DOMElement $e, string $tag): void
601602
* "Fishy" is an algorithm based on content length, classnames,
602603
* link density, number of images & embeds, etc.
603604
*/
604-
public function cleanConditionally(\DOMElement $e, string $tag): void
605+
public function cleanConditionally(DOMElement $e, string $tag): void
605606
{
606607
if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
607608
return;
@@ -714,7 +715,7 @@ public function cleanConditionally(\DOMElement $e, string $tag): void
714715
/**
715716
* Clean out spurious headers from an Element. Checks things like classnames and link density.
716717
*/
717-
public function cleanHeaders(\DOMElement $e): void
718+
public function cleanHeaders(DOMElement $e): void
718719
{
719720
for ($headerIndex = 1; $headerIndex < 3; ++$headerIndex) {
720721
$headers = $e->getElementsByTagName('h' . $headerIndex);
@@ -754,7 +755,7 @@ public function removeFlag(int $flag): void
754755
/**
755756
* Get the article title as an H1.
756757
*
757-
* @return \DOMElement
758+
* @return DOMElement
758759
*/
759760
protected function getArticleTitle()
760761
{
@@ -826,7 +827,7 @@ protected function prepDocument(): void
826827
* Initialize a node with the readability object. Also checks the
827828
* className/id for special names to add to its score.
828829
*/
829-
protected function initializeNode(\DOMElement $node): void
830+
protected function initializeNode(DOMElement $node): void
830831
{
831832
if (!isset($node->tagName)) {
832833
return;
@@ -894,11 +895,11 @@ protected function initializeNode(\DOMElement $node): void
894895
* Using a variety of metrics (content score, classname, element types), find the content that is
895896
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
896897
*
897-
* @param \DOMElement $page
898+
* @param DOMElement $page
898899
*
899-
* @return \DOMElement|false
900+
* @return DOMElement|false
900901
*/
901-
protected function grabArticle(\DOMElement $page = null)
902+
protected function grabArticle(DOMElement $page = null)
902903
{
903904
if (!$page) {
904905
$page = $this->dom;
@@ -1211,7 +1212,7 @@ protected function grabArticle(\DOMElement $page = null)
12111212
if (0 === strcasecmp($tagName, 'td') || 0 === strcasecmp($tagName, 'tr')) {
12121213
$up = $topCandidate;
12131214

1214-
if ($up->parentNode instanceof \DOMElement) {
1215+
if ($up->parentNode instanceof DOMElement) {
12151216
$up = $up->parentNode;
12161217

12171218
if (0 === strcasecmp($up->tagName, 'table')) {
@@ -1340,7 +1341,7 @@ protected function grabArticle(\DOMElement $page = null)
13401341
* Get an element weight by attribute.
13411342
* Uses regular expressions to tell if this element looks good or bad.
13421343
*/
1343-
protected function weightAttribute(\DOMElement $element, string $attribute): int
1344+
protected function weightAttribute(DOMElement $element, string $attribute): int
13441345
{
13451346
if (!$element->hasAttribute($attribute)) {
13461347
return 0;
@@ -1443,14 +1444,14 @@ private function loadHtml(): void
14431444
libxml_use_internal_errors(false);
14441445
}
14451446

1446-
$this->dom->registerNodeClass('DOMElement', 'Readability\JSLikeHTMLElement');
1447+
$this->dom->registerNodeClass(DOMElement::class, \Readability\JSLikeHTMLElement::class);
14471448
}
14481449

1449-
private function getAncestors(\DOMElement $node, int $maxDepth = 0): array
1450+
private function getAncestors(DOMElement $node, int $maxDepth = 0): array
14501451
{
14511452
$ancestors = [];
14521453
$i = 0;
1453-
while ($node->parentNode instanceof \DOMElement) {
1454+
while ($node->parentNode instanceof DOMElement) {
14541455
$ancestors[] = $node->parentNode;
14551456
if (++$i === $maxDepth) {
14561457
break;
@@ -1470,7 +1471,7 @@ private function isPhrasingContent($node): bool
14701471
}, iterator_to_array($node->childNodes)), true));
14711472
}
14721473

1473-
private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
1474+
private function hasSingleTagInsideElement(DOMElement $node, string $tag): bool
14741475
{
14751476
if (1 !== $node->childNodes->length || $node->childNodes->item(0)->nodeName !== $tag) {
14761477
return false;
@@ -1490,7 +1491,7 @@ private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
14901491
* Tidy must be configured to not clean the input for this function to
14911492
* work as expected, see $this->tidy_config['clean']
14921493
*/
1493-
private function isNodeVisible(\DOMElement $node): bool
1494+
private function isNodeVisible(DOMElement $node): bool
14941495
{
14951496
return !($node->hasAttribute('style')
14961497
&& preg_match($this->regexps['isNotVisible'], $node->getAttribute('style'))

0 commit comments

Comments
 (0)