Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 16 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ jobs:
run: composer install --no-interaction

- name: Run tests
run: php -d xdebug.mode=coverage ./vendor/bin/phpunit -c phpunit.ci.xml --coverage-clover=clover.xml
run: composer test:coverage -- --log-junit junit.xml

- name: list files
run: ls -l

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
report_type: test_results

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
if: ${{ !cancelled() }}
uses: codecov/codecov-action@v5
with:
files: ./clover.xml
flags: unittests
name: php-${{ matrix.php-version }}
token: ${{ secrets.CODECOV_TOKEN }}
files: ./clover.xml
flags: tests
15 changes: 9 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/vendor
/.idea
/tests_coverage
/clover.xml
/docs/phpdoc-*
composer.lock
.php_cs.cache
.phpunit.result.cache
/composer.lock

/.phpunit.result.cache
/coverage
.php-cs-fixer.cache
/tests_coverage
/clover.xml
/junit.xml
/codecov

/.php-cs-fixer.cache
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"prefer-stable": true,
"scripts": {
"test": "phpunit",
"test:coverage": "XDEBUG_MODE=coverage phpunit",
"lint": "./vendor/bin/php-cs-fixer check --diff",
"lint:fix": "./vendor/bin/php-cs-fixer fix"
},
Expand Down Expand Up @@ -60,7 +61,8 @@
"jclaveau/xhprof": "dev-master",
"jclaveau/phpunit-profile-asserts": "1.2.0",
"dusank/knapsack": "^10.0",
"friendsofphp/php-cs-fixer": "^3.94"
"friendsofphp/php-cs-fixer": "^3.94",
"doctrine/sql-formatter": "^1.5"
},
"require": {
"php": "^8.0",
Expand Down
30 changes: 0 additions & 30 deletions phpunit.ci.xml

This file was deleted.

2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
</include>
<report>
<html outputDirectory="coverage"/>
<clover outputFile="clover.xml"/>
<!-- <junit outputFile="junit.xml"/> -->
</report>
</coverage>

Expand Down
6 changes: 5 additions & 1 deletion src/Converter/InlineSqlMinimalConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public function addParameter($value) {
public function convert(LogicalFilter $filter) {
$this->output = [];
parent::convert($filter);

return [
'sql' => ! $this->output
? '1' // True
? (! $this->simplifiedFilter ? 'TRUE' : 'FALSE')
: '('.implode(') OR (', $this->output).')',
'parameters' => $this->parameters,
];
Expand Down Expand Up @@ -130,6 +131,9 @@ public function onAndPossibility($field, $operator, $rule, array $allOperandsByF
elseif ('string' == gettype($value)) {
$value = $this->addParameter($value);
}
elseif ('boolean' == gettype($value)) {
$value = $value ? 'TRUE' : 'FALSE';
}
elseif ('array' == gettype($value)) {
$sql_part = [];
foreach ($value as $possibility) {
Expand Down
12 changes: 8 additions & 4 deletions src/Converter/MinimalConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@

use JClaveau\LogicalFilter\Converter\ConverterInterface;
use JClaveau\LogicalFilter\LogicalFilter;
use JClaveau\LogicalFilter\Rule\AbstractOperationRule;

/**
* Basic tools to implements minimal converters: Converters that can
* handle simplified filters with or/and/atomic structure.
*/
abstract class MinimalConverter implements ConverterInterface {
protected ?AbstractOperationRule $simplifiedFilter = null;

/**
* @param LogicalFilter $filter
*/
public function convert(LogicalFilter $filter) {
$rootOr = $filter->simplify(['force_logical_core' => true])->getRules();
$this->simplifiedFilter = $filter->simplify(['force_logical_core' => true])->getRules();

// Simplified filter with no rules
// TODO remove this once TrueRule implemented https://github.com/jclaveau/php-logical-filter/issues/59
if (null === $rootOr) {
if (null === $this->simplifiedFilter) {
return $this;
}

if ( ! $rootOr->hasSolution()) {
if ( ! $this->simplifiedFilter->hasSolution()) {
return $this;
}

foreach ($rootOr->getOperands() as $andOperand) {
foreach ($this->simplifiedFilter->getOperands() as $andOperand) {
$this->onOpenOr();
$operandsByFields = $andOperand->groupOperandsByFieldAndOperator();

Expand Down
4 changes: 2 additions & 2 deletions src/LogicalFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,10 @@ public function applyOn($data_to_filter, $action_on_matches = null, $filterer =
}

/**
* Applies the current instance to a value (and its index optionnally).
* Applies the current instance to a value (and its index in an array optionnally).
*
* @param mixed $value_to_check
* @param scalar $index
* @param scalar $key_to_check If a rule tests the index of an entry in an array
* @param Filterer|callable|null $filterer
*
* @return AbstractRule|false|true + False if the filter doesn't validates
Expand Down
33 changes: 33 additions & 0 deletions src/Rule/AndRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ protected static function simplifyDifferentOperandsForField($field, array $opera
if ( ! empty($operandsByOperator[BelowOrEqualRule::operator])) {
$belowOrEqualRule = $operandsByOperator[BelowOrEqualRule::operator][0];

// with <
if ( ! empty($operandsByOperator[BelowRule::operator])) {
$upper_limit = reset($operandsByOperator[BelowRule::operator])->getUpperLimit();

Expand All @@ -742,6 +743,7 @@ protected static function simplifyDifferentOperandsForField($field, array $opera
}
}

// with >
if ( ! empty($operandsByOperator[AboveRule::operator])) {
$lower_limit = reset($operandsByOperator[AboveRule::operator])->getLowerLimit();

Expand All @@ -751,6 +753,7 @@ protected static function simplifyDifferentOperandsForField($field, array $opera
}
}

// with >=
if ( ! empty($operandsByOperator[AboveOrEqualRule::operator])) {
$minimum = reset($operandsByOperator[AboveOrEqualRule::operator])->getMinimum();

Expand All @@ -771,6 +774,36 @@ protected static function simplifyDifferentOperandsForField($field, array $opera
}
}

// Comparison between >= and > or <
if ( ! empty($operandsByOperator[AboveOrEqualRule::operator])) {
$minimum_or_equal = reset($operandsByOperator[AboveOrEqualRule::operator])->getMinimum();

// with <
if ( ! empty($operandsByOperator[BelowRule::operator])) {
$upper_limit = reset($operandsByOperator[BelowRule::operator])->getUpperLimit();

if ($minimum_or_equal >= $upper_limit) {
// [field < 3] && [field >= 4] <=> false
return [];
}
}

// with >
if ( ! empty($operandsByOperator[AboveRule::operator])) {
$lower_limit = reset($operandsByOperator[AboveRule::operator])->getLowerLimit();

if ($minimum_or_equal >= $lower_limit) {
// [field > 3] && [field >= 3]
// [field > 3] && [field >= 4]
unset($operandsByOperator[AboveOrEqualRule::operator][0]);
}
else {
// [field > 3] && [field >= 2]
unset($operandsByOperator[AboveRule::operator][0]);
}
}
}

return $operandsByOperator;
}

Expand Down
Loading