Pregunta

The following is an example requesting an explanation for one specific file in one specific filesystem, not helper classes generally.

I have configured a LEPP stack on a CentOS server. The server hosts an API which is built using Slim PHP Framework. There is an official skeleton repo on Slim's github.

The repo has a basic file structure which is clearly defined as follows:

app
├───dependencies.php
├───middleware.php
├───repositories.php
├───routes.php
└───settings.php
logs
└───app.log
public
└───index.php
src
├───Application
│   ├───Actions
│   │   ├───User
│   │   │   ├───ListUsersAction.php
│   │   │   ├───UserAction.php
│   │   │   └───ViewUserAction.php
│   │   ├───Action.php
│   │   ├───ActionError.php
│   │   └───ActionPayload.php
│   ├───Handlers
│   │   ├───HttpErrorHandler.php
│   │   └───ShutdownHandler.php
│   ├───Middleware
│   │   └───SessionMiddleware.php
│   └───ResponseEmitter
│       └───ResponseEmitter.php
├───Domain
│   ├───DomainException
│   │   ├───DomainException.php
│   │   └───DomainRecordNotFoundException.php
│   └───User
│       ├───User.php
│       ├───UserNotFoundException.php
│       └───UserRepository.php
└───Infrastructure
│   └───Persistence
│       └───User
│           └───InMemoryUserRepository.php
tests
var
└───cache

I have added my own endpoints to this, for example a 'ViewWordAction' class which utilises a 'PostgresWordRepository' class and returns dictionary definitions for a single word found within a database table when the /dict?word={word} endpoint.

This action is excluded from the file structure for clarity. There are various other endpoints which I will add, used to analyse word data.

I have a separate class called 'RegExHelper' which separates punctuation, recognises ends of sentences and other similar functions. In previous versions of my application, the file structure was a mess and did not follow proper conventions, so this and similar classes were stored in a 'Helper' folder within the src directory. This class will be shared amongst various other classes across multiple endpoints.

I would like to know where to store this helper class and which naming conventions to use. I presume that the class should be refactored as 'RegExHander' and stored in a src/Application/Handlers/RegEx directory, although I cannot find any guides or documentation which explain the proper file structure for this example.

¿Fue útil?

Solución

Where to store Helper classes and what naming conventions to use is largely a matter of taste. There is no consensus; some developers consider Helper classes an anti-pattern and don't use them at all.

The general principles I follow:

  1. Put the Helper class in a namespace or folder that is consistent with its scope (i.e. that Helper class is used throughout the namespace you put it in).
  2. If the Helper class applies to the entire program, put it in a Helpers namespace or folder.
  3. Put a Helper suffix on all helper classes that are static and have static members.
  4. Move code to helper classes when you can express it as pure functions or methods (i.e. they have no side effects and return the same result every time).
Licenciado bajo: CC-BY-SA con atribución
scroll top