PHPackages                             xtain/psr6-null - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. xtain/psr6-null

ActiveLibrary[Caching](/categories/caching)

xtain/psr6-null
===============

PSR-6 cache NullObject implementation, to avoid null checks and for testing

3.0.0(4y ago)047MITPHPPHP &gt;=8.0

Since Feb 26Pushed 4y agoCompare

[ Source](https://github.com/XTAIN/psr6-null)[ Packagist](https://packagist.org/packages/xtain/psr6-null)[ RSS](/packages/xtain-psr6-null/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (5)Used By (0)

PSR-6 NullObject cache
======================

[](#psr-6-nullobject-cache)

The missing PSR-6 NullObject implementation.

You can use this package, when you want to

- avoid using `null` check logic, read more [here](http://designpatternsphp.readthedocs.org/en/latest/Behavioral/NullObject/README.html)
- need a fake cache implementation for testing

Install
-------

[](#install)

```
composer require xtain/psr6-null

```

Example / usage
---------------

[](#example--usage)

Before this package, you needed to allow `null` as a parameter, if you wanted to avoid a package dependency to a specific `PSR-6 cache implementation`

### Old code

[](#old-code)

```
namespace MyPackage;

use Psr\Cache\CacheItemPoolInterface;

class MyCode
{

    public function setCache(CacheItemPoolInterface $cache = null)
    {
        $this->cache = $cache;
    }

    /**
     * Can return an instance of null, which is bad!
     *
     * @return null CacheItemPoolInterface
     */
    public function getCache()
    {
        return $this->cache;
    }

    private function internalHeavyMethod()
    {
        $cacheKey = 'myKey';

        // you need to check first, if there is a cache instance around
        if ($this->getCache() !== null && $this->getCache()->hasItem($cacheKey) === true) {
            // cache is available + it has a cache hit!
            return $this->getCache()->getItem($cacheKey);
        }

        $result = do_something_heavy();

        // you need to check first, if there is a cache instance around
        if ($this->getCache() !== null) {
            $item = $this->getCache()->getItem($cacheKey);
            $item->set($result);
            $this->getCache()->save($item);
        }

        return $result;
    }
}
```

### New code

[](#new-code)

```
namespace MyPackage;

use Psr\Cache\CacheItemPoolInterface;
use XTAIN\Psr\Cache\NullCacheItemPool;
use XTAIN\Psr\Cache\CacheItemPoolAwareInterface;
use XTAIN\Psr\Cache\CacheItemPoolAwareTrait;

class MyCode implements CacheItemPoolAwareInterface
{
    use CacheItemPoolAwareTrait;

    /**
     * You could require a cache instance, so you can remove the null check in __construct() as well
     */
    public function __construct()
    {
        $this->cache = new NullCacheItemPool();
    }

    /**
     * @return CacheItemPoolInterface
     */
    public function getCache()
    {
        return $this->cache;
    }

    private function internalHeavyMethod()
    {
        $cacheKey = 'myKey';

        if ($this->getCache()->hasItem($cacheKey) === true) {
            // cache is available + it has a cache hit!
            return $this->getCache()->getItem($cacheKey);
        }

        $result = do_something_heavy();

        $item = $this->getCache()->getItem($cacheKey);
        $item->set($result);
        $this->getCache()->save($item);

        return $result;
    }
}
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 76.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

1626d ago

Major Versions

1.0.0 → 2.0.02022-01-19

2.0.0 → 3.0.02022-01-19

PHP version history (2 changes)1.0.0PHP &gt;=7.0

2.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2603c0ac94b5622ce4c91c5788f4634bccffe2abdacedd5f732627dbd4809a09?d=identicon)[XTAIN](/maintainers/XTAIN)

---

Top Contributors

[![ThaDafinser](https://avatars.githubusercontent.com/u/533017?v=4)](https://github.com/ThaDafinser "ThaDafinser (16 commits)")[![Deltachaos](https://avatars.githubusercontent.com/u/679561?v=4)](https://github.com/Deltachaos "Deltachaos (4 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (1 commits)")

### Embed Badge

![Health badge](/badges/xtain-psr6-null/health.svg)

```
[![Health](https://phpackages.com/badges/xtain-psr6-null/health.svg)](https://phpackages.com/packages/xtain-psr6-null)
```

###  Alternatives

[symfony/cache

Provides extended PSR-6, PSR-16 (and tags) implementations

4.2k373.5M3.3k](/packages/symfony-cache)[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M339](/packages/api-platform-core)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[api-platform/metadata

API Resource-oriented metadata attributes and factories

275.0M219](/packages/api-platform-metadata)[laminas/laminas-cache

Caching implementation with a variety of storage options, as well as codified caching strategies for callbacks, classes, and output

1077.3M153](/packages/laminas-laminas-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
