diff --git a/Client.php b/Client.php index 14eff151..2fa4fba9 100644 --- a/Client.php +++ b/Client.php @@ -78,7 +78,7 @@ public function isFollowingRedirects() } /** - * Sets the maximum number of requests that crawler can follow. + * Sets the maximum number of redirects that crawler can follow. * * @param int $maxRedirects */ @@ -89,7 +89,7 @@ public function setMaxRedirects($maxRedirects) } /** - * Returns the maximum number of requests that crawler can follow. + * Returns the maximum number of redirects that crawler can follow. * * @return int */ diff --git a/Cookie.php b/Cookie.php index a2201c9c..480f02b9 100644 --- a/Cookie.php +++ b/Cookie.php @@ -44,14 +44,14 @@ class Cookie /** * Sets a cookie. * - * @param string $name The cookie name - * @param string $value The value of the cookie - * @param string $expires The time the cookie expires - * @param string $path The path on the server in which the cookie will be available on - * @param string $domain The domain that the cookie is available - * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client - * @param bool $httponly The cookie httponly flag - * @param bool $encodedValue Whether the value is encoded or not + * @param string $name The cookie name + * @param string $value The value of the cookie + * @param string|null $expires The time the cookie expires + * @param string|null $path The path on the server in which the cookie will be available on + * @param string $domain The domain that the cookie is available + * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client + * @param bool $httponly The cookie httponly flag + * @param bool $encodedValue Whether the value is encoded or not */ public function __construct(string $name, ?string $value, string $expires = null, string $path = null, string $domain = '', bool $secure = false, bool $httponly = true, bool $encodedValue = false) { @@ -112,8 +112,8 @@ public function __toString() /** * Creates a Cookie instance from a Set-Cookie header value. * - * @param string $cookie A Set-Cookie header value - * @param string $url The base URL + * @param string $cookie A Set-Cookie header value + * @param string|null $url The base URL * * @return static * @@ -242,7 +242,7 @@ public function getRawValue() /** * Gets the expires time of the cookie. * - * @return string The cookie expires time + * @return string|null The cookie expires time */ public function getExpiresTime() { diff --git a/CookieJar.php b/CookieJar.php index 232cefc8..7a4d64c1 100644 --- a/CookieJar.php +++ b/CookieJar.php @@ -43,32 +43,21 @@ public function get($name, $path = '/', $domain = null) { $this->flushExpiredCookies(); - if (!empty($domain)) { - foreach ($this->cookieJar as $cookieDomain => $pathCookies) { - if ($cookieDomain) { - $cookieDomain = '.'.ltrim($cookieDomain, '.'); - if ($cookieDomain != substr('.'.$domain, -strlen($cookieDomain))) { - continue; - } - } - - foreach ($pathCookies as $cookiePath => $namedCookies) { - if ($cookiePath != substr($path, 0, strlen($cookiePath))) { - continue; - } - if (isset($namedCookies[$name])) { - return $namedCookies[$name]; - } + foreach ($this->cookieJar as $cookieDomain => $pathCookies) { + if ($cookieDomain && $domain) { + $cookieDomain = '.'.ltrim($cookieDomain, '.'); + if ($cookieDomain !== substr('.'.$domain, -\strlen($cookieDomain))) { + continue; } } - return; - } - - // avoid relying on this behavior that is mainly here for BC reasons - foreach ($this->cookieJar as $cookies) { - if (isset($cookies[$path][$name])) { - return $cookies[$path][$name]; + foreach ($pathCookies as $cookiePath => $namedCookies) { + if (0 !== strpos($path, $cookiePath)) { + continue; + } + if (isset($namedCookies[$name])) { + return $namedCookies[$name]; + } } } } diff --git a/Tests/CookieJarTest.php b/Tests/CookieJarTest.php index 9c9e122e..3117e5ce 100644 --- a/Tests/CookieJarTest.php +++ b/Tests/CookieJarTest.php @@ -237,6 +237,8 @@ public function testCookieGetWithSubdirectory() $this->assertEquals($cookie1, $cookieJar->get('foo', '/test', 'example.com')); $this->assertEquals($cookie2, $cookieJar->get('foo1', '/', 'example.com')); $this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar', 'example.com')); + + $this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar')); } public function testCookieWithWildcardDomain()