Skip to content

Commit

Permalink
Deprecate Statement::create argument usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 18, 2024
1 parent 3ff8654 commit 95987b5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All Notable changes to `Csv` will be documented in this file
- `ByteSequence` Interface use the `Bom` enum instead
- `Info::fetchBOMSequence` use `Bom::tryFromSequence` instead
- `League\Csv\Doctrine` use the new `League\Csv\Constraint` feature instead
- `League\Csv\Statement::create` arguments; The method should be used without any argument at all. All arguments will be removed in the next major version.

### Fixed

Expand Down
42 changes: 41 additions & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
use function array_reduce;
use function array_search;
use function array_values;
use function debug_backtrace;
use function end;
use function is_string;
use function func_num_args;
use function trigger_error;

use const DEBUG_BACKTRACE_IGNORE_ARGS;
use const E_USER_DEPRECATED;

/**
* Criteria to filter a {@link TabularDataReader} object.
Expand All @@ -47,16 +54,49 @@ class Statement
protected array $select = [];

/**
* @param ?callable(array, array-key): bool $where, Deprecated argument use Statement::where instead
* @param int $offset, Deprecated argument use Statement::offset instead
* @param int $limit, Deprecated argument use Statement::limit instead
*
* @throws Exception
* @throws InvalidArgument
* @throws ReflectionException
*/
public static function create(?callable $where = null, int $offset = 0, int $limit = -1): self
{
if (0 === func_num_args()) {
return new self();
}

self::triggerDeprecation('9.16.0', 'Using arguments with the `' . __METHOD__ . '` method is deprecated');
$stmt = new self();
if (null !== $where) {
$stmt = $stmt->where($where);
}

return $stmt->offset($offset)->limit($limit);
if (0 !== $offset) {
$stmt = $stmt->offset($offset);
}

if (-1 !== $limit) {
$stmt = $stmt->limit($limit);
}

return $stmt;
}

private static function triggerDeprecation(string $version, string $message): void
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$caller = end($backtrace);
if (isset($caller['file'])) {
$message .= '; called from ' . $caller['file'];
}
if (isset($caller['line'])) {
$message .= ' on line ' . $caller['line'];
}

@trigger_error('Since league\csv '.$version.': '.$message . '.', E_USER_DEPRECATED);
}

/**
Expand Down

0 comments on commit 95987b5

Please sign in to comment.