Skip to content

Commit

Permalink
Refactoring and min PHP version
Browse files Browse the repository at this point in the history
- major refactoring of the code
- PHP7.4 as min PHP version
- update deps
- add some code from #3 (thanks @pmacelli)
  • Loading branch information
marcogiovinazzi committed Nov 15, 2021
1 parent a81c7df commit 772cdfa
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 474 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
composer.lock
coverage.clover
.phpunit.result.cache
vendor/
build/*
docs/build/
ocular.phar
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
}
},
"require": {
"php": ">=5.4",
"phpseclib/phpseclib": "^2.0",
"php": ">=7.4",
"phpseclib/phpseclib": "^3.0@stable",
"comodojo/exceptions": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^4.0|^5.0",
"scrutinizer/ocular": "^1.0"
"phpunit/phpunit": "^9.0@stable"
},
"minimum-stability": "dev",
"scripts": {
"test": "sh tests/before.sh && vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover && sh tests/after.sh",
"test-multi": "/usr/bin/bash .docker/test.sh",
"scrutinizer": "wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover"
}
}
175 changes: 68 additions & 107 deletions src/Comodojo/Cookies/AbstractCookie.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Comodojo\Cookies;
<?php

namespace Comodojo\Cookies;

use \Comodojo\Exception\CookieException;

Expand All @@ -20,56 +22,57 @@
* THE SOFTWARE.
*/

abstract class AbstractCookie implements CookieInterface {
abstract class AbstractCookie implements CookieInterface
{

/**
* The cookie name
*
* @var string
*/
protected $name;
protected string $name = '';

/**
* Cookie value (native string or serialized one)
*
* @var string
*/
protected $value;
protected string $value = '';

/**
* Expiration time
*
* @var integer
*/
protected $expire;
protected int $expire = 0;

/**
* Path of cookie
*
* @var string
*/
protected $path;
protected string $path = '';

/**
* Domain of cookie
*
* @var string
*/
protected $domain;
protected string $domain = '';

/**
* Secure flag
*
* @var bool
*/
protected $secure = false;
protected bool $secure = false;

/**
* Httponly flag
*
* @var bool
*/
protected $httponly = false;
protected bool $httponly = false;

/*
* Max cookie size
Expand All @@ -78,7 +81,7 @@ abstract class AbstractCookie implements CookieInterface {
*
* @var int
*/
protected $max_cookie_size;
protected int $max_cookie_size;

/**
* Default cookie's constructor
Expand All @@ -88,206 +91,164 @@ abstract class AbstractCookie implements CookieInterface {
*
* @throws CookieException
*/
public function __construct($name, $max_cookie_size = null) {

try {

$this->setName($name);

} catch (CookieException $ce) {

throw $ce;

}

public function __construct(string $name, int $max_cookie_size = null)
{
$this->setName($name);
$this->max_cookie_size = filter_var($max_cookie_size, FILTER_VALIDATE_INT, [
'options' => [
'default' => CookieInterface::COOKIE_MAX_SIZE
]
]);

}

/**
* {@inheritdoc}
*/
public function setName($name) {

if ( empty($name) || !is_scalar($name) ) throw new CookieException("Invalid cookie name");
public function setName(string $name): CookieInterface
{
if (empty($name)) {
throw new CookieException("Invalid cookie name");
}

$this->name = $name;

return $this;

}

/**
* {@inheritdoc}
*/
public function getName() {

public function getName(): string
{
return $this->name;

}

/**
* {@inheritdoc}
*/
abstract function setValue($value, $serialize);
abstract function setValue($value, bool $serialize = true): CookieInterface;

/**
* {@inheritdoc}
*/
abstract function getValue($unserialize);
abstract function getValue(bool $unserialize = true);

/**
* {@inheritdoc}
*/
public function setExpire($timestamp) {

if ( !is_int($timestamp) ) throw new CookieException("Invalud cookie's expiration time");

public function setExpire(int $timestamp): CookieInterface
{
$this->expire = $timestamp;

return $this;

}

/**
* {@inheritdoc}
*/
public function setPath($location) {

if ( !is_string($location) ) throw new CookieException("Invalid path attribute");

public function setPath(string $location): CookieInterface
{
$this->path = $location;

return $this;

}

/**
* {@inheritdoc}
*/
public function setDomain($domain) {

if ( !is_scalar($domain) || !CookieTools::checkDomain($domain) ) throw new CookieException("Invalid domain attribute");
public function setDomain(string $domain): CookieInterface
{
if (!CookieTools::checkDomain($domain)) {
throw new CookieException("Invalid cookie domain");
}

$this->domain = $domain;

return $this;

}

/**
* {@inheritdoc}
*/
public function setSecure($mode = true) {

$this->secure = filter_var($mode, FILTER_VALIDATE_BOOLEAN);

public function setSecure(bool $mode = true): CookieInterface
{
$this->secure = $mode;
return $this;

}

/**
* {@inheritdoc}
*/
public function setHttponly($mode = true) {

$this->httponly = filter_var($mode, FILTER_VALIDATE_BOOLEAN);

public function setHttponly(bool $mode = true): CookieInterface
{
$this->httponly = $mode;
return $this;

}

/**
* {@inheritdoc}
*/
public function save() {

if ( setcookie(
public function save(): bool
{
if (setcookie(
$this->name,
$this->value,
$this->expire,
$this->path,
$this->domain,
$this->secure,
$this->httponly
) === false ) throw new CookieException("Cannot set cookie: ".$this->name);

) === false) {
throw new CookieException("Cannot set cookie: " . $this->name);
}
return true;

}

/**
* {@inheritdoc}
*/
public function load() {

if ( !$this->exists() ) throw new CookieException("Cookie does not exists");

public function load(): CookieInterface
{
if (!$this->exists()) {
throw new CookieException("Cookie ".$this->name." does not exists");
}
$this->value = $_COOKIE[$this->name];

return $this;

}

/**
* {@inheritdoc}
*/
public function delete() {

if ( !$this->exists() ) return true;

if ( setcookie(
public function delete(): bool
{
if (!$this->exists()) {
return true;
}
if (setcookie(
$this->name,
null,
time() - 86400,
null,
null,
$this->secure,
$this->httponly
) === false ) throw new CookieException("Cannot delete cookie");

) === false) {
throw new CookieException("Cannot delete cookie");
}
return true;

}

/**
* {@inheritdoc}
*/
public function exists() {

public function exists(): bool
{
return isset($_COOKIE[$this->name]);

}

/**
* Static method to quickly delete a cookie
*
* @param string $name
* The cookie name
*
* @return boolean
* @throws CookieException
* {@inheritdoc}
*/
public static function erase($name) {

try {

$class = get_called_class();

$cookie = new $class($name);

return $cookie->delete();

} catch (CookieException $ce) {

throw $ce;

}

public static function erase(string $name): bool
{
$class = get_called_class();
$cookie = new $class($name);
return $cookie->delete();
}

}

0 comments on commit 772cdfa

Please sign in to comment.