Contents

Coding Standards

Introduction

This document outlines the coding standards and best practices for the UchuDocs repository. We follow PSR-12, the extended coding style guide for PHP, to ensure consistency, readability, and maintainability across the codebase.

PSR-12 Coding Style

UchuDocs adheres to the PSR-12: Extended Coding Style standard. This standard extends and expands upon PSR-1: Basic Coding Standard.

Key PSR-12 Guidelines

Files

PHP Tags

<?php

// PHP code here

// No closing tag

Lines

Indentation

Keywords and Types

Namespace and Use Declarations

<?php

namespace Vendor\Package;

use Vendor\Package\ClassA as A;
use Vendor\Package\ClassB;
use Vendor\Package\ClassC as C;

// Code here

Classes, Properties, and Methods

<?php

namespace Vendor\Package;

class ClassName
{
    public $foo = null;

    public function sampleMethod(int $a, int $b = null): array
    {
        // method body
    }
}

Control Structures

<?php

if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body
}

switch ($expr) {
    case 0:
        echo 'First case';
        break;
    default:
        echo 'Default case';
        break;
}

Closures

<?php

$closureWithArgs = function ($arg1, $arg2) {
    // body
};

$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
    // body
};

Additional UchuDocs Standards

Documentation

/**
 * Calculate the total price of items
 *
 * @param array $items Array of items with 'price' attribute
 * @return float The total price
 * @throws InvalidArgumentException If an item doesn't have a price
 */
public function calculateTotal(array $items): float
{
    // Implementation
}

Error Handling

Security

Testing

public function testCalculateTotalSumsPricesCorrectly(): void
{
    // Arrange
    $items = [
        ['price' => 10.0],
        ['price' => 20.0]
    ];
    $calculator = new PriceCalculator();

    // Act
    $result = $calculator->calculateTotal($items);

    // Assert
    $this->assertEquals(30.0, $result);
}

Git Workflow

Branches

Commits

Example:

feat: implement user authentication
fix: resolve memory leak in data processing
docs: update API documentation

Pull Requests

Code Review Guidelines

Continuous Integration/Deployment

Conclusion

Following these PSR-12 standards and additional guidelines will help maintain a high-quality, consistent codebase for the UchuDocs repository. These guidelines should evolve with the project as new technologies and best practices emerge.

Join our Discord
Discord Community