Skip to content

Commit

Permalink
Improve typehinting (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b authored Jan 29, 2021
1 parent c40aca5 commit 8f58938
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",

"require": {
"php": ">=7.2",
"php": ">=7.4",
"phpgt/dom": "2.*",
"phpgt/session": "1.*"
},
Expand Down
79 changes: 47 additions & 32 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/ArrayTokenStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* session - it has no other way of remembering the tokens!
*/
class ArrayTokenStore extends TokenStore {
private $arrayStore = [];
/** @var array<string, ?int> */
private array $arrayStore = [];

public function __construct(int $maxTokens = null) {
parent::__construct($maxTokens);
Expand Down
4 changes: 2 additions & 2 deletions src/HTMLDocumentProtector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class HTMLDocumentProtector {
*/
const TOKEN_NAME = "csrf-token";

private $document;
private $tokenStore;
private HTMLDocument $document;
private TokenStore $tokenStore;

public function __construct(
HTMLDocument $document,
Expand Down
9 changes: 3 additions & 6 deletions src/TokenStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
* the tokens is provided by the base class, but can be overridden.
*/
abstract class TokenStore {
/**
* @var int|null The maximum number of tokens to be retained.
*/
protected $maxTokens = 1000;
protected $tokenLength = 32;
protected $tokenGenerator;
protected ?int $maxTokens = 1000;
protected int $tokenLength = 32;

/**
* An optional limit of the number of valid tokens the TokenStore will retain may be passed.
Expand Down Expand Up @@ -57,6 +53,7 @@ public function generateNewToken():string {
* If a $_POST global exists, check that it contains a token and that the token is valid.
* The name the token is stored-under is contained in HTMLDocumentProtector::TOKEN_NAME.
*
* @param array<string, int>|callable $postData
* @throws CsrfTokenMissingException There's a $_POST request present but no
* token present
* @throws CsrfTokenInvalidException There's a token included on the $_POST,
Expand Down

0 comments on commit 8f58938

Please sign in to comment.