📝
xoops-modules-cookbook
  • Read me
  • Introduction
  • Templates
  • Database
  • Coding Standards
    • Our Recommendations
      • PSR-12 Style Guide
      • Clean Code
      • Module Structure
  • Best Practices
    • Best Practices
      • Convention over Configuration
      • Add namespaces
      • Add Sample Buttons
      • Add Comment Notification
      • Add Comments
      • How to handle AND/OR in CriteriaCompo
  • Core Classes
    • Classes
      • Key Classes
      • Helper Class
      • Criteria
      • XoopsObject
      • XoopsObject & XoopsObjectHandler
      • XoopsObjectHandler & XoopsPersistableObjectHandler
  • Working with Database
    • Using Criteria classes
  • Common Classes/Traits
    • Common Classes
      • Breadcrumb
      • Configurator
      • Migrate
    • Traits
      • FilesManagement
      • ServerStats
      • VersionChecks
      • ModuleStats
  • Form Classes
    • Form Classes
      • XoopsButtonTray
      • XoopsForm
      • XoopsFormButton
      • XoopsFormButtonTray
      • XoopsFormCaptcha
      • XoopsFormCheckBox
      • XoopsFormColorPicker
      • XoopsFormDateTime
      • XoopsFormDhtmlTextArea
      • XoopsFormEditor
      • XoopsFormElement
      • XoopsFormElementTray
      • XoopsFormFile
      • XoopsFormHidden
      • XoopsFormHiddenToken
      • XoopsFormLabel
      • XoopsFormPassword
      • XoopsFormRadio
      • XoopsFormRadioYN
      • XoopsFormSelect
      • XoopsFormSelectCheckGroup
      • XoopsFormSelectCountry
      • XoopsFormSelectEditor
      • XoopsFormSelectGroup
      • XoopsFormSelectLang
      • XoopsFormSelectMatchOption
      • XoopsFormSelectTheme
      • XoopsFormSelectTimeZone
      • XoopsFormSelectUser
      • XoopsFormText
      • XoopsFormTextArea
      • XoopsFormTextDateSelect
      • XoopsSimpleForm
      • XoopsTableForm
      • XoopsThemeForm
      • XoopsGroupPermForm
      • XoopsGroupFormCheckBox
  • Migration
    • Namespaces/Autoload
  • Configuration
    • ModuleConfiguration
      • Configuration files
  • Testing
    • Testing Checklist
    • Testing
  • About
    • Credits
    • About XOOPS CMS
Powered by GitBook
On this page
  • Principles and rules
  • Naming
  • Database Conventions
  • Common rules
  • Dependency injection
  • Tools
  • Read more
  • File Uploads

Was this helpful?

  1. Best Practices
  2. Best Practices

Convention over Configuration

PreviousBest PracticesNextAdd namespaces

Last updated 3 years ago

Was this helpful?

We don't want you to waste time when you program, so we follow the principles of .

Following these rules will also make it easier to maintain your code in the future by other developers.

Principles and rules

Here are the most important rules:

  • - Keep it simple, stupid

  • - You Arent Gonna Need It

  • - Don’t repeat yourself

  • - The First 5 Principles of Object Oriented Design

  • - Leave your code better than you found it

Naming

  • Use english names only (class names, method names, comments, variables names, database table and field names, etc…).

  • Use class suffixes / prefixes according to .

  • Follow industry best practices for your directory and file structure:

    • - A skeleton repository for packages

    • - Names for your root-level directories

Database Conventions

  • Entity property names are used for column names.

  • Entity properties that are named ID or classname ID are recognized as primary key properties.

  • A property is interpreted as a foreign key property if it's named <navigation property name><primary key property name> (for example, StudentID for the Student navigation property since the Student entity's primary key is ID). Foreign key properties can also be named the same - simply <primary key property name> (for example, EnrollmentID since the Enrollment entity's primary key is EnrollmentID).

Common rules

  • Methods without return statement must declared with void as their return type.

  • Don’t mix data types for parameters and return types, except for nullable.

  • Don’t extend classes or create abstract classes for the sake of “code reuse”, except for traits with test code only.

  • Create final classes by default, except you have to mock it (e.g. repositories).

  • Create private methods by default. Don’t create protected methods.

  • All method parameters must be used.

Dependency injection

  • Use composition over inheritance.

  • Declare all class dependencies only within the constructor.

  • Don’t inject the container (PSR-11). The service locator is an anti-pattern.

  • A constructor can accept only dependencies as object.

  • Scalar data types (string, int, float, array) are not allowed for the constructor. Pass them as parameter object.

Tools

  • Use a static code analyzer to detect bugs and errors. For example:

Read more

  • http://bestpractices.thecodingmachine.com/

  • https://odan.github.io/learn-php/

File Uploads

  • For files that are uploaded by users, each module should use the /uploads folder

Table names should be singular, e.g. when we have a User entity class, the table in the database should be named user by default. See the reasoning behind it

All methods must have and return type declaration.

Class properties must have (PHP 7.4+).

In this section we used materials from various authors, incl.

Conventions Over Configuration
KISS
YAGNI
DRY
SOLID
The Boy Scout Rule
PSR Naming Conventions
thephpleague/skeleton
pds/skeleton
here
type declaration
typed properties
phpstan
Php Inspections EA Extended
SonarLint Plugin
Object Design Style Guide
Hexagonal Architecture
Daniel Opitz