phpcomposer-phpcompatibility

Composer install error "Declaration of Maatwebsite\Excel\Cache\MemoryCache::get(string $key, mixed $default = null)"


I have laravel composer file with libs:

"require": {
        "php": "^8.1",
        "ext-intl": "*",
        "ext-openssl": "*",
        "ext-zip": "*",
        "arielmejiadev/larapex-charts": "^5.2",
        "barryvdh/laravel-dompdf": "^2.0",
        "diglactic/laravel-breadcrumbs": "^7.1",
        "doctrine/dbal": "^3.1",
        "guzzlehttp/guzzle": "^7.0.1",
        "intervention/validation": "^3.2",
        "jorijn/laravel-security-checker": "^2.3",
        "kwn/number-to-words": "^2.4",
        "laravel/framework": "^v9.0",
        "laravel/tinker": "^2.5",
        "league/oauth2-facebook": "^2.2",
        "maatwebsite/excel": "^3.1",
        "madorin/matex": "^1.0",
        "pbmedia/laravel-ffmpeg": "^8.1",
        "phpoffice/phpspreadsheet": "^1.22",
        "revolution/laravel-google-sheets": "^6.0",
        "spatie/laravel-activitylog": "^4.5",
        "spatie/laravel-sql-commenter": "^1.2"
    },

It works fine on local, and two other servers, but on one server we have error during instalation:

PHP Fatal error: Declaration of Maatwebsite\Excel\Cache\MemoryCache::get(string $key, mixed $default = null): mixed must be compatible with PsrExt\SimpleCache\CacheInterface::get($key, $default = <default>) in /var/www/html/livepartners_v3.com/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62

If I install "psr/simple-cache": "2.0", it will work, but why in 10 computers it works with "psr/simple-cache": "3.0" installed automaticaly and only in 1 server I have this issee with this lib.

Compared functions:

<?php

namespace Psr\SimpleCache;

interface CacheInterface
{
    /**
     * Fetches a value from the cache.
     *
     * @param string $key     The unique key of this item in the cache.
     * @param mixed  $default Default value to return if the key does not exist.
     *
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
     *
     * @throws \Psr\SimpleCache\InvalidArgumentException
     *   MUST be thrown if the $key string is not a legal value.
     */
    public function get(string $key, mixed $default = null): mixed;

It is the same everything as we have in Memory cache:


namespace Maatwebsite\Excel\Cache;

use Psr\SimpleCache\CacheInterface;

class MemoryCache implements CacheInterface

public function get(string $key, mixed $default = null): mixed
    {
        if ($this->has($key)) {
            return $this->cache[$key];
        }

        return $default;
    }

Something happens when it runs scripts after installing and I get error

It works fine if run composer update --no-script but anyway later I need dump-autoload and I will get the same error.

How it parse this simple cache and get wrong function PsrExt\SimpleCache\CacheInterface::get($key, $default = <default>) without any types if it is realy installed with types and how it works fine on other servers?

PHP updated to 8.1.21 version, also tried to update composer, clean composer cache, remove vendor, doesn't help.

Tried different PHP versions, it works in 8.1.9, but even update to last 8.1.21 doesn't help.

Tried install, tried update, tried with lock file and without lock files all the same error.


Solution

  • Finally, I identified the issue. The problem was with the php-psr module installed on the server. This module had its own implementation of the PSR simple CacheInterface, and Composer was comparing the function in the module instead of the one installed in the vendor folder.

    After removing the php-psr module from the server, everything started working fine again.