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

PSR-15 Support #105

Merged
merged 31 commits into from
Sep 13, 2019
Merged

PSR-15 Support #105

merged 31 commits into from
Sep 13, 2019

Conversation

l0gicgate
Copy link
Member

Closes #102

@l0gicgate l0gicgate marked this pull request as ready for review September 9, 2019 19:44
@l0gicgate l0gicgate added this to the 1.0.0 milestone Sep 9, 2019
@l0gicgate
Copy link
Member Author

@adriansuter sorry, this is a big PR. Had to literally rewrite everything..

@l0gicgate l0gicgate changed the title [WIP] - PSR-15 Support PSR-15 Support Sep 9, 2019
Copy link

@adriansuter adriansuter left a comment

Choose a reason for hiding this comment

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

Wonderful. Some comments.

src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Outdated Show resolved Hide resolved
src/Guard.php Show resolved Hide resolved
@l0gicgate
Copy link
Member Author

@adriansuter great review. Will fix all of it and re-request approval. Thank you for your attention to detail 😂

Copy link

@adriansuter adriansuter left a comment

Choose a reason for hiding this comment

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

Readme review.

README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@adriansuter
Copy link

phpstan gives the following output. Should we tackle those?

 ------ -------------------------------------------------------------------------------------------------------
  Line   Guard.php
 ------ -------------------------------------------------------------------------------------------------------
  135    Variable $_SESSION in isset() always exists and is not nullable.
  156    Property Slim\Csrf\Guard::$failureHandler (callable) does not accept (callable)|null.
  285    Parameter #1 $var of function count expects array|Countable, array|ArrayAccess given.
  424    Elseif condition is always true.
  426    Parameter #2 $pair of method Slim\Csrf\Guard::appendTokenToRequest() expects array, array|null given.

Copy link

@adriansuter adriansuter left a comment

Choose a reason for hiding this comment

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

Appreciate your work. Thanks. I still have some minor comments.

src/Guard.php Show resolved Hide resolved
src/Guard.php Show resolved Hide resolved
@l0gicgate
Copy link
Member Author

l0gicgate commented Sep 10, 2019

@adriansuter there's only a couple PHPStan errors to fix but I think this is fine honestly:

------ -------------------------------------------------------------------------------------------------------
  Line   Guard.php
 ------ -------------------------------------------------------------------------------------------------------
  135    Variable $_SESSION in isset() always exists and is not nullable.
  285    Parameter #1 $var of function count expects array|Countable, array|ArrayAccess given.
  426    Parameter #2 $pair of method Slim\Csrf\Guard::appendTokenToRequest() expects array, array|null given.

Variable $_SESSION in isset() always exists and is not nullable.

That's actually not true, if session_start() hasn't been called, isset($_SESSION) returns false

Parameter #1 $var of function count expects array|Countable, array|ArrayAccess given.

Not sure if we can do a workaround for this, it's because we use count($this->storage). Can we figure out how to make this work for both objects/arrays?

Parameter #2 $pair of method Slim\Csrf\Guard::appendTokenToRequest() expects array,

PHPStan sucks, that's all I can say. It doesn't parse the condition.

@adriansuter
Copy link

That's actually not true, if session_start() hasn't been called, isset($_SESSION) returns false

While you are right, there is a scenario where $_SESSION might be set but the session does not exist. For example if the session was started and later on destroyed. In this case we should maybe rather use

        if (session_status() !== PHP_SESSION_ACTIVE) {

Not sure if we can do a workaround for this, it's because we use count($this->storage). Can we figure out how to make this work for both objects/arrays?

How about

        if (is_array($this->storage) && empty($this->storage)) {
            return null;
        }
        if ($this->storage instanceof ArrayAccess) {
            if (!($this->storage instanceof Countable)) {
                throw new RuntimeException('The Csrf storage is not countable.');
            }
            if (count($this->storage) < 1) {
                return null;
            }
        }

@l0gicgate
Copy link
Member Author

@adriansuter instead of this:

        if ($this->storage instanceof ArrayAccess) {
            if (!($this->storage instanceof Countable)) {
                throw new RuntimeException('The Csrf storage is not countable.');
            }
            if (count($this->storage) < 1) {
                return null;
            }
        }

I went with making ArrayAccess, Iterator and Countable mandatory. I don't even know why we're going through these hoops, it should just be a mandatory array.. It's adding so much complexity for no reason with little added flexibility.

This is what the getLastKeyPair() logic looks like instead..

        if (
            (is_array($this->storage) && empty($this->storage))
            || ($this->storage instanceof Countable && count($this->storage) < 1)
        ) {
            return null;
        }

Copy link

@adriansuter adriansuter left a comment

Choose a reason for hiding this comment

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

Great work!!

@l0gicgate l0gicgate merged commit 827c915 into slimphp:master Sep 13, 2019
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.

Add Slim 4 PSR-15 Support
3 participants