Must be of type float, string given on calculateEnergy

Created on 24 March 2025, about 2 months ago

Problem/Motivation

When running cron manually, following errors is thrown.

TypeError: Drupal\radioactivity\RadioactivityProcessor::calculateEnergy(): Argument #1 ($energy) must be of type float, string given, called in /var/www/html/docroot/modules/contrib/radioactivity/src/RadioactivityProcessor.php on line 308 in Drupal\radioactivity\RadioactivityProcessor->calculateEnergy() (line 348 of modules/contrib/radioactivity/src/RadioactivityProcessor.php).

Steps to reproduce

Select the "Run cron" option on admin toolbar

Proposed resolution

Remaining tasks

User interface changes

Nothing

API changes

Nothing

Data model changes

Nothing

πŸ› Bug report
Status

Active

Version

4.1

Component

Code

Created by

πŸ‡ΉπŸ‡·Turkey burak.ikiler

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @burak.ikiler
  • πŸ‡ΉπŸ‡·Turkey burak.ikiler

    Created a simple patch to fix the issues. It works well as long as energy and timestamp aren’t null or complex types like arrays/objects.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    That section of code should only be called if you are using a Radioactivity field instead of a Radioactivity Reference field.

    In 4.1.x, you can't use a Radioactivity field unless you've upgraded an old site from 8.x-3.x and you were using the Radioactivity field in 8.x-3.x. Using the Radioactivity field is something that is supported only for backwards compatibility, but it is not allowed on new installs.

    Does this correctly describe your situation?

    Rather than using a cast, I would prefer to use getValue() so that the datatype of the energy and timestamp variables is maintained. They are declared as float and integer respectively, in the database schema.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    I moved the patch into an MR to that it can be tested.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
  • πŸ‡³πŸ‡±Netherlands sysosmaster

    @tr the issues stems from how PHP no longer allows old school type coercion when a specific type is enforced in a method deceleration.

    I.e: the code changed by @Sutharsan in commit 1390df1149620130b83bbbb2740b846aabb0e112 has now changed meaning where it requirs the inputs to be explicitly typecast to floats / ints respectively.

    you cannot depand on the database layer to do this for you since it doesn't (always) typecasts values. (it depands on its implementation and what type it is)

    just casting it as this MR does would work in most cases, a better solution is to use the proper functions for it that also strip out any offending characters (since the logic of this function should be going away).

    this can be done like this:

     $newEnergy = $this->calculateEnergy(floatval($field->energy), intval($field->timestamp), $profile, $halfLife);
    

    As for test examples to work on, I have the following data that triggered this bug in JSON format:

    {"field->energy":"50","field->timestamp":"1741205501","profile":"decay","halflife":null} 
    {"field->energy":"10","field->timestamp":"1741205502","profile":"decay","halflife":null} 
    {"field->energy":"20","field->timestamp":"1741205503","profile":"decay","halflife":null} 
    {"field->energy":"10","field->timestamp":"1741205504","profile":"decay","halflife":null} 
    {"field->energy":"10","field->timestamp":"1741205505","profile":"decay","halflife":null} 
    {"field->energy":"70","field->timestamp":"1741205506","profile":"decay","halflife":null} 
    {"field->energy":"3585.97","field->timestamp":"1741205507","profile":"decay","halflife":null} 
    {"field->energy":"40","field->timestamp":"1741205508","profile":"decay","halflife":null} 
    {"field->energy":"3585.97","field->timestamp":"1741205509","profile":"decay","halflife":null} 
    {"field->energy":"40","field->timestamp":"1741205510","profile":"decay","halflife":null}
    

    I hope you now have sufficient information to process this ticket.

Production build 0.71.5 2024