Skip to content
Advertisement

Annotations: How to specify timestamps in docblocks in php

I have a mehtod that returns timestamps. I want to do something like this:

class MyAwesomeService {
    
    /**
     * @return array<int, timestamp>
     */
    public function myAwesomeMethod(): array
    {
        return [
            1636380000,    
            1636385555,
            1636386666,
        ];
    }
}

However, I don’t think @return array<int, timestamp> is valid.

What is the valid format for specifying timestamps in docblocks?

Advertisement

Answer

You can use int[], there is no valid value for timestamps. You can however create a ValueObject.

class MyAwesomeService {
    
    /**
     * @return int[]
     */
    public function myAwesomeMethod(): array
    {
        return [
            1636380000,    
            1636385555,
            1636386666,
        ];
    }
}

If you use a value object:

final class Timestamp
{
    private $timestamp;
    public function __construct(int $timestamp) {
        $this->timestamp = $timestamp; 
    }
    
    public function get() : int 
    {
        return $this->timestamp;
    }
}

class MyAwesomeService {
    
    /**
     * @return Timestamp[]
     */
    public function myAwesomeMethod(): array
    {
        return [
            new Timestamp(1636380000),    
            new Timestamp(1636385555),
            new Timestamp(1636386666),
        ];
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement