step 1

$date = date("Y-m-d", $timestamp) // it works perfectly

step 2

install phpstan


turns out my $timestamp is a float, date() expects an int

step 3

how the heck is that possible?

simple. i do some math with the timestamp and get its result with ceil() which is a function that ceils a float to an int. but what kind of int? an integer number, like 1, 2, 3, etc, but it actually returns it as a float because float can store bigger numbers even if they are integer numbers.

anyways, simple solution:

(int)$timestamp

step 4

do i want to cast it every time in my whole codebase, like this?

it's time to extract this.

function ceilInt($float): int

would perfectly solve it in 1 or 2 lines with the body, right?

right.

but it can't be autoloaded because it's not in a class

so it should be in a Math class

class Math {
  function ceilInt(float): int
}

but i dont want to use static functions as they are against proper testability right?

and i dont want to instantiate the Math every time I use it

so a DIContainer is necessary or something similar (lets stick with the DIContainer)

I wrote one x years ago so lets use that

oh uh it's a bit complex, best time for some testing

so let's setup phpunit but this time use docker for that

wild error messages appear (probably makes things hard that i can't use the latest php so documents usually differ from what i have to do)

no worries lets retry it 10 times with different approaches

okay got tired need a little break

moral of the story: how many devops engineers/architects does it need to display a date in php in 2024?

note: even the (int) casting was unnecessary as a starting point

anyways, testing is good. at some point when it couldnt install even the fucking phpunit, i started to write a test framework for a sec, but then i dropped the idea after 5 lines of dealing with different path structures on different environments (i could actually implement it, it's just too much hassle)

...

in the end that's how I ended up with a nice unit test framework. (i find joy in writing my own solutions if it wasn't obvious by now.)

in our next episode: what happened to the date which worked perfectly fine (as the language is intentionally built around dynamic types) before/despite the static analysis?


RSS: hozzászólások ehhez a bejegyzéshez Szólj hozzá

Hozzászólásod:


Nem vagy bejelentkezve, de...

A)
hozzászólhatsz regisztrálatlanul...

neved:

B)
ha regisztrálva vagy, bejelentkezhetsz...
usernév ::
jelszó ::