Skip to content

Various minor code & phpdoc improvements#734

Closed
jtojnar wants to merge 6 commits intosimplepie:masterfrom
jtojnar:psalm-fixes
Closed

Various minor code & phpdoc improvements#734
jtojnar wants to merge 6 commits intosimplepie:masterfrom
jtojnar:psalm-fixes

Conversation

@jtojnar
Copy link
Copy Markdown
Member

@jtojnar jtojnar commented May 19, 2022

I tried using Psalm to fix some random issues and improve the codebase a bit with regards to static analysis.

jtojnar added 6 commits May 18, 2022 17:25
- Most of the properties are nullable, update the annotations to match reality.
- Use more specific types for arrays.
- Add param types to constructor.
- `length` is int in practice (there are no fractional bytes), make the annotations int instead of string and float.
- Fix some undefined variables.
- Remove redundant explicit namespaces.
- Most of the properties are nullable, update the annotations to match reality.
- Use more specific types for arrays.
- Fix passing false to strpos/substr where they expect int.
- Remove redundant explicit namespaces.
- Add missing/fix type PHPDoc type annotations.
while ($row = $query->fetchColumn())
{
$feed['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['entry'][] = unserialize($row);
assert(is_string($row));
Copy link
Copy Markdown
Contributor

@Art4 Art4 May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. Isn't the outcome of assert() probably not determinable because of different configuration of the server?

Assertions should be used as a debugging feature only. [...] Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.

https://www.php.net/manual/en/function.assert.php

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertions are only used here as a hint for the static analyser – they should never trigger, unless the database is corrupted. But I do not really like them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that help the static analyser?

- assert(is_string($row));
+ $row = (string) $row;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would make the type checker happy I assume. Thought I find this somewhat iffy too, since it casts a variable to string that already is a string (unless db is corrupted). Assertions, meanwhile, will be disabled on production.

I know this will have negligible performance impact but it is still a smelly code. Though maybe it is acceptable trade off, if we want static analysis to be happy.


/**
* @var string Original feed URL, or new feed URL iff HTTP 301 Moved Permanently
* @var string|null Original feed URL, or new feed URL iff HTTP 301 Moved Permanently
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also fix the typo (iff -> if)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iff is a short for equivalence.

$query->bindValue(':id', $this->id);
if ($query->execute() && ($time = $query->fetchColumn()))
{
assert(is_int($time));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about assert().

@mblaney
Copy link
Copy Markdown
Member

mblaney commented May 19, 2022

looking good @jtojnar

@jtojnar jtojnar marked this pull request as ready for review May 19, 2022 12:03
@jtojnar jtojnar marked this pull request as draft May 19, 2022 13:23
@jtojnar
Copy link
Copy Markdown
Member Author

jtojnar commented May 19, 2022

Thinking about this, I do not really like this PR – the commits are too scattered and hard to review, even for me as the author.

There are also stylistic changes mixed in. Namely, I converted absolute namespaces to relative ones in PHPDoc because Psalm for some reason did not like those (probably a bug). We should probably use php-cs-fixer to deal with those first.

I moved the only two clean commits to #735

@jtojnar jtojnar closed this May 19, 2022
@Art4
Copy link
Copy Markdown
Contributor

Art4 commented May 20, 2022

What do you think about using php-cs-fixer to change the code style to PSR-12? I think that the timing is ideal for that at the moment.

@Art4
Copy link
Copy Markdown
Contributor

Art4 commented May 23, 2022

@jtojnar Would you like create a PR about PSR-12 change or should I create it?

@jtojnar
Copy link
Copy Markdown
Member Author

jtojnar commented May 23, 2022

@Art4 I probably will not be able to get to it anytime soon, so feel free to do it.

@Art4 Art4 mentioned this pull request May 23, 2022
@Art4 Art4 mentioned this pull request Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants