Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Improves CI #28

Merged
merged 23 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e8e187e
Fixes not ignored root file
DarkGhostHunter Jul 25, 2022
a8669c8
Fixes runs
DarkGhostHunter Jul 25, 2022
6593052
Fixes steps
DarkGhostHunter Jul 25, 2022
c94b112
Fixes exported files
DarkGhostHunter Jul 26, 2022
c6b84b4
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
12a7025
Apply fixes from StyleCI
StyleCIBot Jul 26, 2022
fefacb6
Merge pull request #25 from Laragear/analysis-KoonP3
DarkGhostHunter Jul 26, 2022
bb90619
Fixed wrong null token
DarkGhostHunter Jul 26, 2022
a46bb0c
Apply fixes from StyleCI
StyleCIBot Jul 26, 2022
ad98db3
Merge pull request #26 from Laragear/analysis-4ww3kE
DarkGhostHunter Jul 26, 2022
f2d9c3c
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
a624dbe
Apply fixes from StyleCI
DarkGhostHunter Jul 26, 2022
7e61b1f
Merge pull request #27 from Laragear/analysis-prrYOL
DarkGhostHunter Jul 26, 2022
8f1f99a
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
38e1342
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
8659880
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
3e212ac
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
cb76ede
Should have fixed static analysis
DarkGhostHunter Jul 26, 2022
6a218c9
Should have fixed tests.
DarkGhostHunter Jul 26, 2022
af74584
Should have fixed static analysis.
DarkGhostHunter Jul 26, 2022
62e8bc8
Ignores unreachable return line.
DarkGhostHunter Jul 26, 2022
57a79a7
Apply fixes from StyleCI
DarkGhostHunter Jul 26, 2022
50e9de7
Merge pull request #29 from Laragear/analysis-O33WaW
DarkGhostHunter Jul 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/phpunit.xml export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.assets export-ignore
/.styleci.yml export-ignore
/composer.lock export-ignore
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
141 changes: 109 additions & 32 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,127 @@
name: Tests
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow

name: "Tests"

on:
push:
branches:
- "feat/improve-ci" # TODO: Remove once the PR is ready to be merged.
pull_request:

jobs:
test:

runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [ 8.0, 8.1 ]
laravel: [ 9.* ]
dependency-version: [ prefer-stable, prefer-lowest ]
include:
- laravel: 9.*
testbench: 7.*

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.dependency-version }}
byte_level:
name: "0️⃣ Byte-level"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Check file permissions"
run: |
test "$(find . -type f -not -path './.git/*' -executable)" == ""
- name: "Find non-printable ASCII characters"
run: |
! LC_ALL=C.UTF-8 find ./src -type f -name "*.php" -print0 | xargs -0 -- grep -PHn "[^ -~]"
syntax_errors:
name: "1️⃣ Syntax errors"
runs-on: "ubuntu-latest"
steps:
- name: Checkout
uses: actions/checkout@v2
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
tools: "parallel-lint"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"

- name: Setup PHP
uses: shivammathur/setup-php@v2
- name: "Check source code for syntax errors"
run: "composer exec -- parallel-lint src/"

unit_tests:
name: "2️⃣ Unit and Feature tests"
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
strategy:
matrix:
php-version:
- "8.1"
laravel-constrain:
- "9.*"
dependencies:
- "lowest"
- "highest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: ${{ matrix.php }}
php-version: "${{ matrix.php-version }}"
extensions: mbstring, intl
coverage: xdebug

- name: Cache dependencies
uses: actions/cache@v2
- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
path: ~/.composer/cache/files
key: ${{ runner.os }}-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
restore-keys: ${{ runner.os }}-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-
dependency-versions: "${{ matrix.dependencies }}"

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-progress --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress
- name: Run Tests
run: composer run-script test
- name: "Execute unit tests"
run: "composer run-script test"

- name: "Upload coverage to Codecov"
uses: "codecov/codecov-action@v2"

static_analysis:
name: "3️⃣ Static Analysis"
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
tools: "phpstan"

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Validate Composer configuration"
run: "composer validate --strict"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"

- name: "Execute static analysis"
run: "composer exec -- phpstan analyze -l 5 src/"

exported_files:
name: "4️⃣ Exported files"
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Check exported files"
run: |
EXPECTED="LICENSE.md,README.md,composer.json"
CURRENT="$(git archive HEAD | tar --list --exclude="src" --exclude="src/*" --exclude=".stubs" --exclude=".stubs/*" --exclude="lang" --exclude="lang/*" --exclude="config" --exclude="config/*" --exclude="database" --exclude="database/*" --exclude="resources" --exclude="resources/*" | paste -s -d ",")"
echo "CURRENT =${CURRENT}"
echo "EXPECTED=${EXPECTED}"
test "${CURRENT}" == "${EXPECTED}"
13 changes: 6 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
build
/build
/vendor
/.idea
.php-cs-fixer.cache
.phpunit.result.cache
composer.lock
docs
vendor
coverage
.idea
/.phpunit.result.cache
/phpunit.xml.dist.bak
phpunit.xml.bak
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This package enables TOTP authentication using 6 digits codes. No need for exter

## Keep this package free

[![](.assets/patreon.png)](https://patreon.com/packagesforlaravel)[![](.assets/ko-fi.png)](https://ko-fi.com/DarkGhostHunter)[![](.assets/buymeacoffee.png)](https://www.buymeacoffee.com/darkghosthunter)[![](.assets/paypal.png)](https://www.paypal.com/paypalme/darkghosthunter)
[![](.github/assets/patreon.png)](https://patreon.com/packagesforlaravel)[![](.github/assets/ko-fi.png)](https://ko-fi.com/DarkGhostHunter)[![](.github/assets/buymeacoffee.png)](https://www.buymeacoffee.com/darkghosthunter)[![](.github/assets/paypal.png)](https://www.paypal.com/paypalme/darkghosthunter)

Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **[spread the word!](http://twitter.com/share?text=I%20am%20using%20this%20cool%20PHP%20package&url=https://github.com%2FLaragear%2FTwoFactor&hashtags=PHP,Laravel)**

Expand Down
7 changes: 4 additions & 3 deletions src/Models/Concerns/HandlesCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ protected function codeHasBeenUsed(string $code): bool
*/
protected function setCodeAsUsed(string $code, DateTimeInterface|int|string $at = 'now'): void
{
$timestamp = Carbon::createFromTimestamp($this->getTimestampFromPeriod($at, $this->window + 1));

// We will safely set the cache key for the whole lifetime plus window just to be safe.
$this->cache->set($this->cacheKey($code), true,
Carbon::createFromTimestamp($this->getTimestampFromPeriod($at, $this->window + 1))
);
// @phpstan-ignore-next-line
$this->cache->set($this->cacheKey($code), true, $timestamp);
}
}
4 changes: 2 additions & 2 deletions src/Models/Concerns/HandlesRecoveryCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ trait HandlesRecoveryCodes
/**
* The custom generator to make recovery codes.
*
* @var (callable(int, int, int): \Illuminate\Support\Collection<int, int|string>)
* @var (callable(int, int, int): \Illuminate\Support\Collection<int, int|string>)|null
*/
protected static $generator;
protected static $generator = null;

/**
* Returns if there are Recovery Codes available.
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Concerns/SerializesSharedSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function toUri(): string
'secret' => $this->shared_secret,
'algorithm' => strtoupper($this->attributes['algorithm']),
'digits' => $this->attributes['digits'],
], null, '&', PHP_QUERY_RFC3986);
], '', '&', PHP_QUERY_RFC3986);

return 'otpauth://totp/'.rawurlencode($issuer).'%3A'.$this->attributes['label']."?$query";
}
Expand Down
6 changes: 2 additions & 4 deletions src/Models/TwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @property null|\Illuminate\Support\Carbon|\DateTime $updated_at
* @property null|\Illuminate\Support\Carbon|\DateTime $created_at
*
* @method static \Database\Factories\Laragear\TwoFactor\TwoFactorAuthenticationFactory factory()
* @method static \Database\Factories\Laragear\TwoFactor\TwoFactorAuthenticationFactory<static> factory($count = null, $state = [])
*/
class TwoFactorAuthentication extends Model implements TwoFactorTotp
{
Expand Down Expand Up @@ -141,9 +141,7 @@ public static function generateRandomSecret(): string
}

/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory<static>
* @inheritDoc
*/
protected static function newFactory(): Factory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Totp
/**
* Create a new "totp code" rule instance.
*
* @param \Illuminate\Contracts\Auth\Authenticatable|\Laragear\TwoFactor\Contracts\TwoFactorAuthenticatable|null $user
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
*/
public function __construct(protected ?Authenticatable $user = null)
{
Expand Down
10 changes: 7 additions & 3 deletions src/TwoFactorLoginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
use Laragear\TwoFactor\Exceptions\InvalidCodeException;
use function response;
use function view;
Expand Down Expand Up @@ -144,6 +143,10 @@ public function attempt(array $credentials = [], $remember = false): bool

$this->throwConfirmView($this->input, $this->request->has($this->input) ? $e->errors() : []);
}

// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -165,11 +168,10 @@ protected function getSessionGuard(): SessionGuard
/**
* Retrieve the flashed credentials in the session, and merges with the new on top.
*
* @param array $credentials
* @param array{credentials:array, remember:bool} $credentials
* @param mixed $remember
* @return array
*/
#[ArrayShape(['credentials' => 'array', 'remember' => 'bool'])]
protected function getFlashedData(array $credentials, mixed $remember): array
{
$original = $this->session->get("$this->sessionKey.credentials", []);
Expand All @@ -195,6 +197,7 @@ protected function flashData(array $credentials, bool $remember): void
$credentials[$key] = Crypt::encryptString($value);
}

// @phpstan-ignore-next-line
$this->session->flash($this->sessionKey, ['credentials' => $credentials, 'remember' => $remember]);
}

Expand All @@ -207,6 +210,7 @@ protected function flashData(array $credentials, bool $remember): void
*/
protected function throwConfirmView(string $input, array $errors): void
{
// @phpstan-ignore-next-line
response(view($this->view, ['input' => $input])->withErrors($errors))->throwResponse();
}
}
2 changes: 2 additions & 0 deletions src/TwoFactorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ protected function publishFiles(): void
$this->publishesMigrations(static::DB);

$this->publishes([static::CONFIG => $this->app->configPath('two-factor.php')], 'config');
// @phpstan-ignore-next-line
$this->publishes([static::VIEWS => $this->app->viewPath('vendor/two-factor')], 'views');
// @phpstan-ignore-next-line
$this->publishes([static::LANG => $this->app->langPath('vendor/two-factor')], 'translations');
}
}