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

Small improvements found by my phpstan work #9661

Closed
wants to merge 23 commits into from
Closed

Small improvements found by my phpstan work #9661

wants to merge 23 commits into from

Conversation

sminnee
Copy link
Member

@sminnee sminnee commented Aug 27, 2020

This is a trimming back of a rebased version of PHPStan PR, #9103.

The work of preparing that led me to find lots of little things that could be tidied up that, while they would require PR, are safe changes.

I've moved all the 'dangerous' changes out of that PR but figure it's useful to get these into mainline so that they are less likely to rot.

In particular, I note that @sunnysideup has started some PHPStan work in parallel and it's a bit of a shame that this work was rotting on a branch.

The original PR still has 5 commits not included in this.

@szepeviktor
Copy link
Contributor

@Cheddam Could you help us?

@@ -503,7 +503,7 @@ public static function send_file($fileData, $fileName, $mimeType = null)
* The pattern can optionally start with an HTTP method and a space. For example, "POST $Controller/$Action".
* This is used to define a rule that only matches on a specific HTTP method.
*
* @param $pattern
* @param string$pattern
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we need one more whitespace.

@@ -136,6 +136,9 @@ public function __construct(
$titleConfirmField = null
) {

// This param is requested only for backward compat
$form;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe ignoreErrors: is a better place than this noop.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like this better.

* Returns false if it's a bad data type, and if appropriate, throws an exception.
*
* @param SS_List $dataList
* @return bool
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be true.

if ($dataList instanceof Filterable) {
return true;

} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for else.

{
if ($dataList instanceof Filterable && $dataList instanceof Limitable) {
return $dataList;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for else.

return $this->list->limit($limit, $offset);
if ($this->list instanceof Limitable) {
return $this->list->limit($limit, $offset);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

else!

return $this->list->byID($id);
if ($this->list instanceof Filterable) {
return $this->list->byID($id);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

else!

return $this->list->byIDs($ids);
if ($this->list instanceof Filterable) {
return $this->list->byIDs($ids);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

else!

return $this->list->exclude(...func_get_args());
if ($this->list instanceof Filterable) {
return $this->list->exclude(...$arguments);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

else!

return $this->list->debug();
if ($this->list instanceof ViewableData) {
return $this->list->debug();
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

else!

Copy link
Contributor

@szepeviktor szepeviktor left a comment

Choose a reason for hiding this comment

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

Please consider putting errors in conditional and normal execution without indentation.

if (we have a problem) {
    // Error here.
    throw new Exception('💣');
}

return $normal . $execution;

Normal execution in a conditional is hard to read: zig-zag instead of straight down.

Copy link
Member

@chillu chillu left a comment

Choose a reason for hiding this comment

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

Looked through all of this - again, what a mission, well done Sam! Apart from the three review comments I'm happy for this to go in.

protected function checkDataType($dataList)
{
if ($dataList instanceof Filterable && $dataList instanceof Limitable) {
return $dataList;
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit odd that in the delete class you return true, but here you return $dataList. I didn't expect an object return from a check...() function, would prefer the inlined type hinting via @var

Copy link
Member Author

Choose a reason for hiding this comment

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

The point was to return something of a known type but fair enough an @var works too.

I might try an assert() or something as well...

Copy link
Member Author

Choose a reason for hiding this comment

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

OK so assert()s work with PHPStan: https://phpstan.org/r/9b405a35-5203-45f7-9cd0-2e611d9e2331

I'm checking PHPStorm now too.

Copy link
Member Author

Choose a reason for hiding this comment

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

PHPStorm and VS Code both use assert(... instanceof ...) commands to augment type-hinting.

I'll use that.

Copy link
Member Author

@sminnee sminnee Sep 16, 2020

Choose a reason for hiding this comment

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

assert() will throw a warning but unless the php.ini setting assert.exception is set to true it won't break execution. I guess that's okay?

I'd note that /** @var */ is the worst possible solution here as it doesn't affect code execution. It just helps your IDE lie to you. :-P

src/ORM/DataObject.php Show resolved Hide resolved
/**
* In error condition, set transactionNesting to zero
*/
protected function resetTransactionNesting()
Copy link
Member

Choose a reason for hiding this comment

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

With our new semver rules, that's technically an API breakage. What's the motivation for this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right I'll pull the change out of this PR and into the other one for more in-depth discussion

Sam Minnee added 18 commits September 16, 2020 18:58
Untidiness picked up on by phpstan
These never worked, it seems
These variables are always set
Subsystems relying on DataObjectInterface made use of these methods but
didn’t make it explicit.

This is essentially a bugfix on DataObjectInterface, although if someone
had implemented DataObjectInterface without ViewableData, they will
need to implement some new methods as part of this change. However,
their code is quite likely to be broken until they do so. This is also
a rarely used API.#
@sminnee sminnee closed this Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants