Skip to content

Discussion: Interface Discrepancies #4356

@MGatner

Description

@MGatner

Introduction

I want to begin a "big picture" discussion about something that has caused numerous issues in the past: framework interfaces. Let me also point out up front that this is currently a problem. We have done a handful of patch jobs to "make things work" but they have not addressed the underlying issues. A few references for example:

Interfaces

An interface is a contract between the code and our developers:

  1. A class that implements an interface must have all the defined methods.
  2. An interface cannot be changed without a major release.
  3. Anything that uses an interface in a typehint or docblock must use only that interface's methods.

Problem

Number three is where we get in trouble. Due to the nature of frameworks we need to move classes around a lot. In order to allow extending/replacing core components we use interfaces. In our current code base, sometimes this has been done well and maintained and sometimes it has not. Two examples...

Examples

CacheInterface

The CacheInterface is an example of interfacing handled well. The interface has 10 public methods, all the handlers implement those methods without embellishment, and our Cache Service returns the interface:

/**
* The cache class provides a simple way to store and retrieve
* complex data for later.
*
* @param Cache|null $config
* @param boolean $getShared
*
* @return CacheInterface
*/
public static function cache(Cache $config = null, bool $getShared = true)

This means that other components like Throttler can use CacheInterface safely:

/**
* Container for throttle counters.
*
* @var \CodeIgniter\Cache\CacheInterface
*/
protected $cache;

... and any developer who wished to use custom caching can define their own.

ConnectionInterface

I'm going to pick on the database layer because of this recent issue (#4329) and because it is a bit of a mess generally. ConnectionInterface has 16 public methods but they are not nearly comprehensive enough to support all our required database methods so we use BaseConnection implements ConnectionInterface and then have handlers extend BaseConnection. This way when MigrationRunner needs to check for the "migrations" table is can use the embellished method provided by BaseConnection:

/**
* Ensures that we have created our migrations table
* in the database.
*/
public function ensureTable()
{
if ($this->tableChecked || $this->db->tableExists($this->table))
{
return;
}

This works because MigrationRunner knows its protected $db property is a BaseConnection:

/**
* The main database connection. Used to store
* migration information in.
*
* @var BaseConnection
*/
protected $db;

That looks fine, but OOPS! our migration service expects a ConnectionInterface:

/**
* Return the appropriate Migration runner.
*
* @param Migrations|null $config
* @param ConnectionInterface|null $db
* @param boolean $getShared
*
* @return MigrationRunner
*/
public static function migrations(Migrations $config = null, ConnectionInterface $db = null, bool $getShared = true)

Now we have a conundrum. Do we rework dependent classes so they only use the interface methods? Or do we redefine all services to use BaseConnection so we have access to these embellished methods? If we do the latter, then why have ConnectionInterface at all?

Road Forward

Unfortunately ConnectionInterface and MigrationRunner aren't an isolate incident. We have numerous infractions of this time throughout the framework, the ones I am most aware of being in the database and HTTP layers. Given the current "broken" state of things I think we have some leeway in the changes we can make, but since we are not ready for version 5 yet these will have to be handled delicately. Here's my proposal but I'd like to hear ideas from others as well:

  1. Identify comprehensive and intact interfaces (like Cache) and standardize their references through the code
  2. Identify abstract and base classes that should be used instead of interfaces, update all references and deprecated the interfaces
  3. Identify interfaces we want to keep and push intermediate implementations back as far as possible (e.g. no "MyClass extends MyImplementationOfInterface")
  4. Identify non-comprehensive interfaces that cannot be replaced and make new interfaces (extensions) to be used instead

What are your thoughts? criticisms? solutions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions