From f4909f31a91850f65368bb42986a203d39ba437d Mon Sep 17 00:00:00 2001 From: Chris Doehring Date: Sat, 31 Jul 2021 14:43:21 +0200 Subject: [PATCH] feat: add static analysis --- .gitattributes | 2 + .github/workflows/phpcs.yml | 44 +++++++++++++++++++ .github/workflows/phpstan.yml | 44 +++++++++++++++++++ composer.json | 14 +++++- phpcs.xml | 15 +++++++ phpstan.neon | 4 ++ src/Factories/BrowserFactory.php | 3 +- src/Factories/BrowserPolicyFactory.php | 3 +- src/Http/Middleware/SecureResponseCookies.php | 14 ++++-- 9 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/phpcs.yml create mode 100644 .github/workflows/phpstan.yml create mode 100644 phpcs.xml create mode 100644 phpstan.neon diff --git a/.gitattributes b/.gitattributes index 27c2bbc..a0c4d15 100644 --- a/.gitattributes +++ b/.gitattributes @@ -6,4 +6,6 @@ /.gitignore export-ignore /phpunit.xml.dist export-ignore /tests export-ignore +/phpcs.xml export-ignore +/phpstan.neon export-ignore /.github export-ignore diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml new file mode 100644 index 0000000..ca9ba14 --- /dev/null +++ b/.github/workflows/phpcs.yml @@ -0,0 +1,44 @@ +name: phpcs + +on: [push, pull_request] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: + - ubuntu-latest + php: + - 8.0 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('composer.*') }} + restore-keys: | + composer-${{ runner.os }}-${{ matrix.php-version }}- + composer-${{ runner.os }}- + composer- + + - name: Install Composer dependencies + run: | + composer install --no-interaction --no-progress --optimize-autoloader --no-scripts + + - name: Execute phpcs + run: ./vendor/bin/phpcs \ No newline at end of file diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..76e2a38 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,44 @@ +name: phpstan + +on: [push, pull_request] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: + - ubuntu-latest + php: + - 8.0 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('composer.*') }} + restore-keys: | + composer-${{ runner.os }}-${{ matrix.php-version }}- + composer-${{ runner.os }}- + composer- + + - name: Install Composer dependencies + run: | + composer install --no-interaction --no-progress --optimize-autoloader --no-scripts + + - name: Execute phpstan + run: ./vendor/bin/phpstan analyse \ No newline at end of file diff --git a/composer.json b/composer.json index 06f87f7..c65ed28 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,9 @@ }, "require-dev": { "fakerphp/faker": "^1.14", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^0.12.89", + "squizlabs/php_codesniffer": "^3.6" }, "autoload": { "psr-4": { @@ -22,5 +24,15 @@ "psr-4": { "App\\Tests\\": "tests/" } + }, + "scripts": { + "test": [ + "phpunit", + "phpstan analyse", + "phpcs --standard=phpcs.xml" + ], + "unit": "phpunit", + "stan": "phpstan analyse", + "cs": "phpcs --standard=phpcs.xml" } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..31d96c9 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,15 @@ + + + Custom ruleset Based on PSR12 + src + + + + + + + + + + + \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..1ff472c --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: 8 + paths: + - src diff --git a/src/Factories/BrowserFactory.php b/src/Factories/BrowserFactory.php index 7e2672d..cc542e0 100644 --- a/src/Factories/BrowserFactory.php +++ b/src/Factories/BrowserFactory.php @@ -1,4 +1,5 @@ headers->get('User-Agent', '')); } -} \ No newline at end of file +} diff --git a/src/Factories/BrowserPolicyFactory.php b/src/Factories/BrowserPolicyFactory.php index c28fa60..a2d0da3 100644 --- a/src/Factories/BrowserPolicyFactory.php +++ b/src/Factories/BrowserPolicyFactory.php @@ -1,4 +1,5 @@ browserFactory->createByRequest($request)); } -} \ No newline at end of file +} diff --git a/src/Http/Middleware/SecureResponseCookies.php b/src/Http/Middleware/SecureResponseCookies.php index c1edbaf..125bba4 100644 --- a/src/Http/Middleware/SecureResponseCookies.php +++ b/src/Http/Middleware/SecureResponseCookies.php @@ -59,14 +59,20 @@ protected function secureCookies(Response $response, bool $setSecureAttribute): protected function getSecureDuplicatedCookie(Cookie $cookie, bool $setSecureAttribute): Cookie { $sameSite = $cookie->getSameSite(); - if ($this->browserPolicy->canHaveAttributeSameSite() && $setSecureAttribute) { + if ($this->browserPolicy && $this->browserPolicy->canHaveAttributeSameSite() && $setSecureAttribute) { $sameSite = Cookie::SAMESITE_NONE; } return new Cookie( - $cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), - $cookie->getPath(), $cookie->getDomain(), $setSecureAttribute, - $cookie->isHttpOnly(), $cookie->isRaw(), $sameSite + $cookie->getName(), + $cookie->getValue(), + $cookie->getExpiresTime(), + $cookie->getPath(), + $cookie->getDomain(), + $setSecureAttribute, + $cookie->isHttpOnly(), + $cookie->isRaw(), + $sameSite ); } }