Skip to content

[Bug]: Parameter::asNumber() doesn't work with string #2142

@stloyd

Description

@stloyd

What happened?

Not 100% sure it's a bug, but the behaviour is quite strange in a call like ref('number')->isNotNumeric(), and down the road in: Parameter::asNumber():

    public function asNumber(Row $row, FlowContext $context, int|float|null $default = null) : int|float|null
    {
        $result = $this->eval($row, $context);

        if (\is_string($result)) {
            return $default;
        }

        return \is_numeric($result) ? $result : $default;
    }

Why is it confusing? Cause string can be numeric, while this code says already that the string is not numeric and returns the default (null in this case).

How to reproduce?

data_frame()
    ->read(from_csv(__DIR__ . '/data/orders.csv'))
    ->withEntry('is_numeric', \Flow\ETL\DSL\ref('discount')->isNumeric())
    ->withEntry('is_string', \Flow\ETL\DSL\ref('discount')->isType(\Flow\Types\DSL\type_string()))
    ->select('discount', 'is_numeric', 'is_string')
    ->limit(10)
    ->collect()
    ->write(to_output(truncate: false))
    ->run();

Playground snippet

https://flow-php.com/playground/?snippet=dd01d132a808d32ccdf8f2e6871956c6d121543e484f5ccfddab4f864b8d7b2e

Data required to reproduce bug locally

Version

Latest for sure

Relevant error output

Copy and paste any relevant error output (no backticks needed).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions