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

Prevent using strlen & count on empty input #545

Merged
merged 1 commit into from
Oct 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ private function onStart(Process $process) : void

while (null !== $chunk = $stdout->read()) {
foreach (\explode("\n", $chunk) as $chunkLine) {
if (\strlen($chunkLine)) {
if ('' !== $chunkLine) {
$this->logger->debug($chunkLine);
}
}
}

while (null !== $chunk = $stderr->read()) {
foreach (\explode("\n", $chunk) as $chunkLine) {
if (\strlen($chunkLine)) {
if ('' !== $chunkLine) {
$this->logger->error($chunkLine);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function extract(FlowContext $context) : \Generator
}
}

if (\count($rows) > 0) {
if ([] !== $rows) {
yield new Rows(...$rows);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function extract(FlowContext $context) : \Generator
$rowData = \fgetcsv($stream->resource(), $this->charactersReadInLine, $this->separator, $this->enclosure, $this->escape);
}

if (\count($rows)) {
if ([] !== $rows) {
yield new Rows(...$rows);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
public readonly string $startColumn,
public readonly string $endColumn,
) {
if (\strlen($sheetName) === 0) {
if ('' === $sheetName) {
throw new InvalidArgumentException('Sheet name can\'t be empty');
}

if (!\preg_match('/^[A-Z]+$/u', $startColumn)) {

Check warning on line 20 in src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/Columns.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (locked, 8.1, ubuntu-latest)

Escaped Mutant for Mutator "PregMatchRemoveFlags": --- Original +++ New @@ @@ if ('' === $sheetName) { throw new InvalidArgumentException('Sheet name can\'t be empty'); } - if (!\preg_match('/^[A-Z]+$/u', $startColumn)) { + if (!\preg_match('/^[A-Z]+$/', $startColumn)) { throw new InvalidArgumentException(\sprintf('The column `%s` needs to contain only letters.', $startColumn)); } if (!\preg_match('/^[A-Z]+$/u', $endColumn)) {
throw new InvalidArgumentException(\sprintf('The column `%s` needs to contain only letters.', $startColumn));
}

if (!\preg_match('/^[A-Z]+$/u', $endColumn)) {

Check warning on line 24 in src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/Columns.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (locked, 8.1, ubuntu-latest)

Escaped Mutant for Mutator "PregMatchRemoveFlags": --- Original +++ New @@ @@ if (!\preg_match('/^[A-Z]+$/u', $startColumn)) { throw new InvalidArgumentException(\sprintf('The column `%s` needs to contain only letters.', $startColumn)); } - if (!\preg_match('/^[A-Z]+$/u', $endColumn)) { + if (!\preg_match('/^[A-Z]+$/', $endColumn)) { throw new InvalidArgumentException(\sprintf('The column `%s` needs to contain only letters.', $endColumn)); } if ($endColumn < $startColumn) {
throw new InvalidArgumentException(\sprintf('The column `%s` needs to contain only letters.', $endColumn));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function extract(FlowContext $context) : \Generator
$data = [];

foreach ($dataFields as $field) {
if (\count($this->fields) && !\in_array($field->name, $this->fields, true)) {
if ([] !== $this->fields && !\in_array($field->name, $this->fields, true)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public function launch(Pool $pool, string $host) : void

$process->stderr->on('data', function ($chunk) : void {
foreach (\explode("\n", $chunk) as $chunkLine) {
if (\strlen($chunkLine)) {
if ('' !== $chunkLine) {
$this->logger->error($chunkLine);
}
}
});

$process->stdout->on('data', function ($chunk) : void {
foreach (\explode("\n", $chunk) as $chunkLine) {
if (\strlen($chunkLine)) {
if ('' !== $chunkLine) {
$this->logger->debug($chunkLine);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function extract(FlowContext $context) : \Generator
$rowData = \fgets($fileStream->resource());
}

if (\count($rows)) {
if ([] !== $rows) {
yield new Rows(...$rows);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function extract(FlowContext $context) : \Generator

$xmlReader->close();

if (\count($rows)) {
if ([] !== $rows) {
yield new Rows(...$rows);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function process(Rows $rows) : Rows
->mode(SaveMode::Append)
->threadSafe();

if (\count($this->partitionEntries)) {
if ([] !== $this->partitionEntries) {
$dataFrame = $dataFrame->partitionBy(...$this->partitionEntries);
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

function col(string $entry, string ...$entries) : Reference
{
if (\count($entries)) {
if ([] !== $entries) {
return new StructureReference($entry, ...$entries);
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Filesystem/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(string $uri, private readonly array $options = [])
public static function realpath(string $path, array $options = []) : self
{
// "" - empty path is current, local directory
if (!\strlen($path)) {
if ('' === $path) {
/** @phpstan-ignore-next-line */
return new self(\getcwd(), $options);
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public static function realpath(string $path, array $options = []) : self
}

if ($part === '..') {
if (\count($absoluteParts)) {
if ([] !== $absoluteParts) {
\array_pop($absoluteParts);
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function result() : Rows
$entries = $entries->add($aggregator->result());
}

if (\count($entries)) {
if ($entries->count()) {
$rows = $rows->add(new Row($entries));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Partition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ final class Partition implements Serializable

public function __construct(public readonly string $name, public readonly string $value)
{
if (!\strlen($this->name)) {
if ('' === $this->name) {
throw new InvalidArgumentException("Partition name can't be empty");
}

if (!\strlen($this->value)) {
if ('' === $this->value) {
throw new InvalidArgumentException("Partition value can't be empty");
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(Entry ...$entries)
{
$this->entries = [];

if (\count($entries)) {
if ([] !== $entries) {
foreach ($entries as $entry) {
$this->entries[$entry->name()] = $entry;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/ArrayEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
private readonly string $name,
private readonly array $value
) {
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/BooleanEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class BooleanEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, private readonly bool $value)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/FloatEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class FloatEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, private readonly float $value, private readonly int $precision = 6)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/IntegerEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class IntegerEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, private readonly int $value)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class JsonEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, private readonly array $value)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Row/Entry/ListEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public function __construct(
private readonly Type $type,
private readonly array $value
) {
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}

if (\count($value) && !\array_is_list($value)) {
if ([] !== $value && !\array_is_list($value)) {
throw new InvalidArgumentException('Expected list of ' . $type->toString() . ' got array with not sequential integer indexes');
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/NullEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class NullEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/ObjectEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ObjectEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, private readonly object $value)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/StringEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class StringEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, private string $value)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/Entry/StructureEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class StructureEntry implements \Stringable, Entry
*/
public function __construct(private readonly string $name, Entry ...$entries)
{
if (!\strlen($name)) {
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Rows.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function findOne(callable $callable) : ?Row
}
}

if (\count($rows)) {
if ([] !== $rows) {
return \current($rows);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/array-dot/src/Flow/ArrayDot/array_dot.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
function array_dot_steps(string $path) : array
{
if (!\strlen($path)) {
if ('' === $path) {
throw new InvalidPathException("Path can't be empty.");
}

Expand Down Expand Up @@ -119,7 +119,7 @@ function array_dot_set(array $array, string $path, $value) : array
*/
function array_dot_get(array $array, string $path) : mixed
{
if (\count($array) === 0) {
if ([] === $array) {
if (\str_starts_with($path, '?')) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Columns

public function __construct(string ...$columns)
{
if (\count($columns) === 0) {
if ([] === $columns) {
throw new RuntimeException('Columns cannot be empty');
}

Expand Down
Loading