Skip to content

Commit

Permalink
Merge pull request #22 from tomekkowalczyk/DPMMA-2707_update_php_8.1
Browse files Browse the repository at this point in the history
feat: [DPMMA-2707] Update to PHP 8.1
  • Loading branch information
escopecz authored Aug 29, 2024
2 parents 6b879e1 + 09bc7ce commit 0f9bb9e
Show file tree
Hide file tree
Showing 19 changed files with 261 additions and 399 deletions.
11 changes: 11 additions & 0 deletions .ddev/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: mautic-form-submit
type: php
docroot: "examples/simple-email-form/"
php_version: "8.1"
webserver_type: nginx-fpm
xdebug_enabled: false
additional_hostnames: []
additional_fqdns: []
use_dns_when_possible: true
composer_version: "2"
web_environment: []
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build
composer.lock
vendor
.idea
.phpunit.result.cache
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ $result = $form->submit(['f_email' => 'john@doe.email']);

For working example see the `examples` dir.

## Run project
```
ddev start
```
Project url: https://mautic-form-submit.ddev.site/

## Testing

```
Expand All @@ -58,8 +64,6 @@ composer cs
composer phpstan
```

PHPSTAN must be installed globally (`composer global require phpstan/phpstan-shim`) and will run only on PHP 7+.

### Current status

[Travis](https://travis-ci.org/escopecz/mautic-form-submit)
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=8.1",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit" : "^5.7",
"scrutinizer/ocular": "~1.1",
"squizlabs/php_codesniffer": "~2.3"
"phpunit/phpunit" : "^10.5",
"scrutinizer/ocular": "~1.9",
"rector/rector": "^1.2",
"phpstan/phpstan": "^1.11",
"symplify/easy-coding-standard": "^12.3"
},
"autoload": {
"psr-4": {
Expand All @@ -36,7 +38,7 @@
"scripts": {
"test": "phpunit",
"test-coverage": "phpdbg -qrr vendor/bin/phpunit",
"cs": "phpcs --standard=psr2 src/",
"phpstan": "~/.composer/vendor/phpstan/phpstan-shim/phpstan.phar analyse src tests -l 5"
"cs": "vendor/bin/ecs --fix",
"phpstan": "vendor/bin/phpstan analyse src tests -l 5"
}
}
24 changes: 24 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([
__DIR__ . '/examples',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withRootFiles()
->withConfiguredRule(
ArraySyntaxFixer::class,
['syntax' => 'short']
)
->withRules([
NoUnusedImportsFixer::class,
ListSyntaxFixer::class,
]);
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::NAMING,
SetList::TYPE_DECLARATION,
]);
};
60 changes: 14 additions & 46 deletions src/Cookie.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Escopecz\MauticFormSubmit;

/**
Expand All @@ -12,35 +14,25 @@ class Cookie
* because when cookie is set with setCookie
* it's accessible on the next script run only,
* not at the same run.
*
* @var array
*/
protected $store = [];
protected array $store = [];

/**
* Get cookie with FILTER_SANITIZE_STRING
*
* @param string $key
*
* @return string|null
*/
public function get($key)
public function get(string $key): string|false|null
{
if (isset($this->store[$key])) {
return filter_var($this->store[$key], FILTER_SANITIZE_STRING);
return filter_var($this->store[$key], FILTER_SANITIZE_SPECIAL_CHARS);
}

return filter_input(INPUT_COOKIE, $key, FILTER_SANITIZE_STRING);
return filter_input(INPUT_COOKIE, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}

/**
* Get cookie with FILTER_SANITIZE_NUMBER_INT
*
* @param string $key
*
* @return int|null
*/
public function getInt($key)
public function getInt(string $key): int
{
if (isset($this->store[$key])) {
return (int) filter_var($this->store[$key], FILTER_SANITIZE_NUMBER_INT);
Expand All @@ -49,63 +41,39 @@ public function getInt($key)
return (int) filter_input(INPUT_COOKIE, $key, FILTER_SANITIZE_NUMBER_INT);
}

/**
* Set a cookie value
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function set($key, $value)
public function set(string $key, mixed $value): bool
{
$this->store[$key] = $value;

return setcookie($key, $value);
return setcookie($key, (string) $value);
}

/**
* Unset the key from the cookie
*
* @param string $key
*
* @return Cookie
*/
public function clear($key)
public function clear(string $key): static
{
setcookie($key, '', time() - 3600);
setcookie($key, '', ['expires' => time() - 3600]);
unset($_COOKIE[$key]);
unset($this->store[$key]);

return $this;
}

/**
* Returns $_COOKIE
*
* @return array
*/
public function getSuperGlobalCookie()
public function getSuperGlobalCookie(): array
{
return $_COOKIE;
}

/**
* Return all cookies as array merged with current state
*
* @return array
*/
public function toArray()
public function toArray(): array
{
return array_merge($this->getSuperGlobalCookie(), $this->store);
}

/**
* Creates unique cookie file in system tmp dir and returns absolute path to it.
*
* @return string|false
*/
public function createCookieFile()
public function createCookieFile(): string|false
{
return tempnam(sys_get_temp_dir(), 'mauticcookie');
}
Expand Down
47 changes: 12 additions & 35 deletions src/HttpHeader.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,44 @@
<?php

declare(strict_types=1);

namespace Escopecz\MauticFormSubmit;

/**
* HTTP Header Helper
*/
class HttpHeader
{
/**
* Key-valye paries of headers
*
* @var array
*/
private $headers = [];
private array $headers = [];

/**
* Key-valye paries of cookies
*
* @var array
*/
private $cookies = [];
private array $cookies = [];

public function __construct($textHeaders)
{
$this->parse($textHeaders);
}

/**
* @param string $key
*
* @return string|null
*/
public function getHeaderValue($key)
public function getHeaderValue(string $key): ?string
{
return isset($this->headers[$key]) ? $this->headers[$key] : null;
return $this->headers[$key] ?? null;
}

/**
* @param string $key
*
* @return string|null
*/
public function getCookieValue($key)
public function getCookieValue(?string $key): ?string
{
return isset($this->cookies[$key]) ? $this->cookies[$key] : null;
return $this->cookies[$key] ?? null;
}

/**
* Parse text headers and fills in cookies and headers properites
*
* @param string $headers
*/
private function parse($headers)
private function parse(string $headers): void
{
foreach (preg_split('/\r\n|\r|\n/', $headers) as $i => $line) {
if ($i === 0) {
$this->headers['http_code'] = $line;
} else {
list($key, $value) = explode(': ', $line);
[$key, $value] = explode(': ', $line);

if ($key === 'Set-Cookie') {
list($textCookie) = explode(';', $value);
list($cookieKey, $cookieValue) = explode('=', $textCookie);
[$textCookie] = explode(';', $value);
[$cookieKey, $cookieValue] = explode('=', $textCookie);

$this->cookies[$cookieKey] = $cookieValue;
} else {
Expand Down
Loading

0 comments on commit 0f9bb9e

Please sign in to comment.