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

2.x psr log and php multi compat #75

Draft
wants to merge 20 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
db215b0
refactor: Update package to support php >=8 and Psr Log 2 || 3
luislard Nov 3, 2023
83e07a3
ci: remove php 7.x workflow run
luislard Nov 3, 2023
80d0da6
chore: add static analysis for php 8.0 8.1 8.2
luislard Nov 3, 2023
3c14a3f
chore: add static code analysis for different php versions
luislard Nov 3, 2023
0c2fe9c
chore: fixes psalm error
luislard Nov 3, 2023
e9dfe4e
chore: fix psalm and require the test package
luislard Nov 6, 2023
685d0d3
chore: remove deprecation warning in tests
luislard Nov 6, 2023
c3b39a2
chore: remove phpunit warning about config file using outdated schema
luislard Nov 6, 2023
2c9d84e
chore: increase the php unit test matrix to use prefer lowest and reg…
luislard Nov 6, 2023
9a2ad1c
chore: using matrix instead
luislard Nov 8, 2023
1d5b573
chore: be more specific in suportted php versions
luislard Nov 8, 2023
6f2c987
chore: PHP CS will return same result for all versions
luislard Nov 8, 2023
163498e
feat: adds supports for 7.4 and compat with psr/log 1
luislard Nov 28, 2023
2454a6c
ci: added php 7 compat
luislard Nov 28, 2023
942bc0b
chore: return back older php 7 versions be more explicit in the suppo…
luislard Nov 28, 2023
45bdd55
chore: add missing versions
luislard Nov 28, 2023
98a74fc
chore: droping support for older php versions
luislard Nov 28, 2023
8281d3f
chore: fixing CS errors
luislard Nov 28, 2023
9ca121d
chore: fix line length and ignore some complaints in CS
luislard Nov 28, 2023
8e3dba0
chore: add missing comparator
luislard Dec 14, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/php-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-service: [ '72', '73', '74', '80', '81', '82' ]
php-service: [ '74', '80', '81', '82' ]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/php-static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ jobs:
uses: inpsyde/reusable-workflows/.github/workflows/lint-php.yml@main
if: ${{ (github.event_name != 'workflow_dispatch') || ((github.event.inputs.jobs == 'Run all') || (github.event.inputs.jobs == 'Run lint only')) }}
with:
PHP_MATRIX: '["7.2", "7.3", "7.4", "8.0", "8.1", "8.2"]'
PHP_MATRIX: '["7.4", "8.0", "8.1", "8.2"]'

coding-standards-analysis-php:
if: ${{ (github.event_name != 'workflow_dispatch') || ((github.event.inputs.jobs == 'Run all') || (github.event.inputs.jobs == 'Run PHPCS only')) }}
uses: inpsyde/reusable-workflows/.github/workflows/coding-standards-php.yml@main
with:
PHP_VERSION: '8.0'

static-code-analysis-php:
if: ${{ (github.event_name != 'workflow_dispatch') || ((github.event.inputs.jobs == 'Run all') || (github.event.inputs.jobs == 'Run Psalm only')) }}
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-php.yml@main
strategy:
matrix:
php-version: [ '7.4', '8.0', '8.1', '8.2' ]
with:
PHP_VERSION: ${{ matrix.php-version }}
21 changes: 6 additions & 15 deletions .github/workflows/php-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,26 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
php-versions: [ '7.4', '8.0', '8.1', '8.2' ]
dependency-versions: ['highest', 'lowest']

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use coverage?
if: ${{ matrix.php-versions == '7.4' }}
run: echo "USE_COVERAGE=yes" >> $GITHUB_ENV

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: zend.assertions=1, error_reporting=E_ALL, display_errors=On
coverage: ${{ ((env.USE_COVERAGE == 'yes') && 'xdebug') || 'none' }}
coverage: ${{ env.USE_COVERAGE == 'yes' && 'xdebug' || 'none' }}

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency-versions }}

- name: Run unit tests
run: composer tests:unit:${{ ((env.USE_COVERAGE == 'yes') && 'codecov') || 'no-cov' }}
run: composer tests:unit:no-cov

- name: Update coverage
if: ${{ env.USE_COVERAGE == 'yes' }}
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
flags: unittests
verbose: true

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@

# Docker
.github/workflows/docker/php*/*.sh

# Adding a folder to put things temporarily
temp/

# IDE
.idea/

13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@
}
],
"require": {
"php": ">=7.2 < 8.3",
"psr/log": "^1.1.4",
"php": "^7.4||>=8.0 < 8.3",
"psr/log": ">=1.0.1 <2.0||^2.0||^3.0",
"wecodemore/wordpress-early-hook": "^1.1.0",
"monolog/monolog": "^2.3.5"
"monolog/monolog": ">=2.0.1 <3"
},
"require-dev": {
"phpunit/phpunit": "^8.5.33",
"brain/monkey": "^2.6.1",
"mockery/mockery": "^1.3.6",
"mikey179/vfsstream": "~v1.6.11",
"inpsyde/php-coding-standards": "^1",
"vimeo/psalm": "^4.30.0",
"inpsyde/wp-stubs-versions": "dev-latest",
"roots/wordpress-no-content": ">=6.1.1",
"symfony/process": "^v4.4.44",
"globalis/wp-cli-bin": "^2.7.1"
"globalis/wp-cli-bin": "^2.7.1",
"vimeo/psalm": "^5.15",
"phpunit/phpunit": "^9.6"
},
"provide": {
"psr/log-implementation": "1.0.0"
Expand Down Expand Up @@ -88,6 +88,7 @@
},
"scripts": {
"cs": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs",
"cs:fix": "@php ./vendor/bin/phpcbf",
"psalm": "@php ./vendor/vimeo/psalm/psalm --no-suggestions --find-unused-psalm-suppress --no-diff --no-cache --no-file-cache",
"tests:unit": "@php ./vendor/phpunit/phpunit/phpunit --testsuite=unit",
"tests:unit:no-cov": "@php ./vendor/phpunit/phpunit/phpunit --testsuite=unit --no-coverage",
Expand Down
60 changes: 28 additions & 32 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<extensions>
<extension class="Inpsyde\Wonolog\Tests\IntegrationTestsExtension"/>
</extensions>

<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests/unit</directory>
</testsuite>
<testsuite name="integration">
<directory suffix="Test.php">tests/integration</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<extensions>
<extension class="Inpsyde\Wonolog\Tests\IntegrationTestsExtension"/>
</extensions>
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests/unit</directory>
</testsuite>
<testsuite name="integration">
<directory suffix="Test.php">tests/integration</directory>
</testsuite>
</testsuites>
</phpunit>
14 changes: 12 additions & 2 deletions src/Data/FailedLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,25 @@ private function countAttempts(int $ttl = 300): void
|| !isset($attempts[$userIp]['count'])
|| !isset($attempts[$userIp]['last_logged'])
) {
$attempts[$userIp] = ['count' => 0, 'last_logged' => 0];
/**
* @var array<string, array{count: int, last_logged: int}> $data
*/
$data = ['count' => 0, 'last_logged' => 0];
$attempts[$userIp] = $data;
}

/** @psalm-suppress MixedOperand */
$attempts[$userIp]['count']++;
/** @psalm-suppress PropertyTypeCoercion */
/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->attemptsData = $attempts;

/**
* Psalm warns us about count and last_logged possibly being bool to int converted
* We assume the value retrieved when calling get_site_transient is an integer on both
* @psalm-suppress RiskyCast
*/
$count = (int)$attempts[$userIp]['count'];
/** @psalm-suppress RiskyCast */
$lastLogged = (int)$attempts[$userIp]['last_logged'];

/**
Expand Down
4 changes: 4 additions & 0 deletions src/DefaultHandler/LogsFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public static function determineFolder(?string $customFolder = null): ?string
* them, and package could be fully functional even if failures happen.
* Silence looks like best option here.
*
* Also for some reason __return_true seems not to be a valid argument?
* I found this related issue https://github.com/vimeo/psalm/issues/3571
*
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
* @psalm-suppress PossiblyInvalidArgument
*/
set_error_handler('__return_true');

Expand Down
2 changes: 1 addition & 1 deletion src/HookListener/CronDebugListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function registerEventListener(LogActionUpdater $updater): void
}

$cronArray = _get_cron_array();
/** @psalm-suppress DocblockTypeContradiction */
/** @psalm-suppress TypeDoesNotContainType,DocblockTypeContradiction */
if (!$cronArray || !is_array($cronArray)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PsrBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function withDefaultChannel(string $defaultChannel): PsrBridge
*
* phpcs:disable Generic.Metrics.CyclomaticComplexity
*/
public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
// phpcs:enable Generic.Metrics.CyclomaticComplexity
$throwable = null;
Expand Down
139 changes: 139 additions & 0 deletions tests/src/TestLogger/AbstractLoggerV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

declare(strict_types=1);

# -*- coding: utf-8 -*-
/**
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author AuthorName <[email protected]>
* @license GPLv2+
* @link https://www.inpsyde.com
*/

namespace Inpsyde\Wonolog\Tests\TestLogger;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

abstract class AbstractLoggerV1 implements LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function emergency($message, array $context = [])
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function alert($message, array $context = [])
{
$this->log(LogLevel::ALERT, $message, $context);
}

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function critical($message, array $context = [])
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function error($message, array $context = [])
{
$this->log(LogLevel::ERROR, $message, $context);
}

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function warning($message, array $context = [])
{
$this->log(LogLevel::WARNING, $message, $context);
}

/**
* Normal but significant events.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function notice($message, array $context = [])
{
$this->log(LogLevel::NOTICE, $message, $context);
}

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function info($message, array $context = [])
{
$this->log(LogLevel::INFO, $message, $context);
}

/**
* Detailed debug information.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function debug($message, array $context = [])
{
$this->log(LogLevel::DEBUG, $message, $context);
}
}
Loading
Loading