Skip to content

Commit e9573af

Browse files
committed
code style and feedback
1 parent d746f31 commit e9573af

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Exception;
13+
14+
/**
15+
* Base UnexpectedValueException for the Security component.
16+
*
17+
* @author Oliver Hoff <oliver@hofff.com>
18+
*/
19+
class UnexpectedValueException extends \UnexpectedValueException implements ExceptionInterface
20+
{
21+
}

src/Symfony/Component/Security/Csrf/TokenStorage/CookieTokenStorage.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\Cookie;
1515
use Symfony\Component\HttpFoundation\ParameterBag;
16+
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
1617
use Symfony\Component\Security\Csrf\Exception\TokenNotFoundException;
1718

1819
/**
@@ -27,7 +28,7 @@ class CookieTokenStorage implements TokenStorageInterface
2728
/**
2829
* @var array
2930
*/
30-
private $transientTokens;
31+
private $transientTokens = array();
3132

3233
/**
3334
* @var ParameterBag
@@ -45,7 +46,6 @@ class CookieTokenStorage implements TokenStorageInterface
4546
*/
4647
public function __construct(ParameterBag $cookies, $secure)
4748
{
48-
$this->transientTokens = [];
4949
$this->cookies = $cookies;
5050
$this->secure = (bool) $secure;
5151
}
@@ -58,8 +58,8 @@ public function getToken($tokenId)
5858
{
5959
$token = $this->resolveToken($tokenId);
6060

61-
if (!strlen($token)) {
62-
throw new TokenNotFoundException;
61+
if ('' === $token) {
62+
throw new TokenNotFoundException();
6363
}
6464

6565
return $token;
@@ -71,7 +71,7 @@ public function getToken($tokenId)
7171
*/
7272
public function hasToken($tokenId)
7373
{
74-
return strlen($this->resolveToken($tokenId)) > 0;
74+
return '' !== $this->resolveToken($tokenId);
7575
}
7676

7777
/**
@@ -80,8 +80,10 @@ public function hasToken($tokenId)
8080
*/
8181
public function setToken($tokenId, $token)
8282
{
83-
if (!strlen($token)) {
84-
throw new \InvalidArgumentException('Empty tokens are not allowed');
83+
$token = (string) $token;
84+
85+
if ('' === $token) {
86+
throw new InvalidArgumentException('Empty tokens are not allowed');
8587
}
8688

8789
$this->updateToken($tokenId, $token);
@@ -97,7 +99,7 @@ public function removeToken($tokenId)
9799

98100
$this->updateToken($tokenId, '');
99101

100-
return strlen($token) ? $token : null;
102+
return '' === $token ? null : $token;
101103
}
102104

103105
/**
@@ -113,7 +115,6 @@ public function createCookies()
113115
// the problem is the that the value of deleted cookies get set to
114116
// the string "deleted" and not the empty string
115117

116-
// TODO http only?
117118
$name = $this->generateCookieName($tokenId);
118119
$cookies[] = new Cookie($name, $token, 0, null, null, $this->secure, true);
119120
}
@@ -143,7 +144,6 @@ protected function resolveToken($tokenId)
143144
*/
144145
protected function updateToken($tokenId, $token)
145146
{
146-
$token = (string) $token;
147147
$name = $this->generateCookieName($tokenId);
148148

149149
if ($token === $this->cookies->get($name, '')) {

src/Symfony/Component/Security/Csrf/TokenStorage/RequestStackTokenStorage.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Security\Csrf\TokenStorage;
1313

1414
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\Security\Core\Exception\RuntimeException;
16+
use Symfony\Component\Security\Core\Exception\UnexpectedValueException;
1517

1618
/**
1719
* Forwards token storage calls to a token storage stored in the master
@@ -67,18 +69,25 @@ public function getProxiedTokenStorage()
6769
$request = $this->requestStack->getMasterRequest();
6870

6971
if (!$request) {
70-
throw new \RuntimeException('Not in a request context');
72+
throw new RuntimeException('Not in a request context');
7173
}
7274

7375
$storage = $request->attributes->get($this->tokenStorageKey);
7476

75-
// TODO what if storage is set and not an instance of TokenStorageInterface?
76-
// error out? overwrite?
77-
if (!$storage instanceof TokenStorageInterface) {
78-
$storage = $this->factory->createTokenStorage($request);
79-
$request->attributes->set($this->tokenStorageKey, $storage);
77+
if ($storage instanceof TokenStorageInterface) {
78+
return $storage;
8079
}
8180

81+
if ($storage !== null) {
82+
throw new UnexpectedValueException(sprintf(
83+
'Expected null or an instance of "Symfony\\Component\\Security\\Csrf\\TokenStorage\\TokenStorageInterface", got "%s"',
84+
is_object($storage) ? get_class($storage) : gettype($storage)
85+
));
86+
}
87+
88+
$storage = $this->factory->createTokenStorage($request);
89+
$request->attributes->set($this->tokenStorageKey, $storage);
90+
8291
return $storage;
8392
}
8493

src/Symfony/Component/Security/Csrf/TokenStorage/SessionTokenStorageFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Csrf\TokenStorage;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Security\Core\Exception\RuntimeException;
1516

1617
/**
1718
* Creates CSRF token storages based on the requests session.
@@ -30,7 +31,7 @@ class SessionTokenStorageFactory implements TokenStorageFactoryInterface
3031
*/
3132
public function __construct($namespace = null)
3233
{
33-
$this->namespace = $namespace === null ? SessionTokenStorage::SESSION_NAMESPACE : $namespace;
34+
$this->namespace = $namespace === null ? SessionTokenStorage::SESSION_NAMESPACE : (string) $namespace;
3435
}
3536

3637
/**
@@ -40,7 +41,7 @@ public function __construct($namespace = null)
4041
public function createTokenStorage(Request $request) {
4142
$session = $request->getSession();
4243
if (!$session) {
43-
throw new \RuntimeException('Request has no session');
44+
throw new RuntimeException('Request has no session');
4445
}
4546

4647
return new SessionTokenStorage($session, $this->namespace);

0 commit comments

Comments
 (0)