PHP code example of ncac / phpcs-standard

1. Go to this page and download the library: Download ncac/phpcs-standard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ncac / phpcs-standard example snippets


// ❌ Avoided: ambiguous types
function process($data) {
    return $data->value;
}

// ✅ Required: explicit types
function process(DataObject $data): string {
  return $data->value;
}

// ✅ Function arguments formatting
$result = my_function([
  'clean' => 'readable',
  'consistent' => 'maintainable'
]);

// ✅ Nested arrays maintain hierarchy
$config = [
  'database' => [
    'host' => 'localhost',
    'port' => 3306
  ]
];

// ✅ Consistent naming across contexts
class UserRepository {

  private string $connectionString;  // camelCase property

  public function findUser(int $user_id): User {  // snake_case parameter
    $query_result = $this->executeQuery($user_id);  // snake_case variable
    return $this->mapToUser($query_result);  // camelCase method
  }

}

class userRepository
{

    function findUser($userId)
    {
        $queryResult = $this->executeQuery($userId);
        if ($queryResult) {
            return $queryResult;
        }
        return null;
    }

    private $connectionString;
    const CACHE_TTL = 3600;


}

class UserRepository {

    private string $connectionString;

    public const CACHE_TTL = 3600;

    public function findUser(int $user_id): ?User {
      $query_result = $this->executeQuery($user_id);
      if ($query_result !== null) {
        return $this->mapToUser($query_result);
      }
      return null;
    }

}

// ✅ Valid Drupal preprocess hook (with allowDoubleUnderscore enabled)
function mymodule_preprocess_node__homepage(array &$variables): void {
  // Targets node--homepage.html.twig template
}

// ✅ Valid Drupal theme suggestions hook
function mymodule_theme_suggestions_paragraph__alter(array &$suggestions): void {
  $suggestions[] = 'paragraph__custom';
}

// ✅ Valid internal helper function (with allowLeadingUnderscore enabled)
function _mymodule_internal_helper(string $data): string {
  return strtoupper($data);
}

// Original: if ($x): ... endif;
// After sniff A: if ($x) { ... }  // Fixed
// After sniff B: if ($x) { ...    // Broken (missing closing brace)
bash
# Step 1: Apply PHP-CS-Fixer for complex transformations
vendor/bin/php-cs-fixer fix src/ --config=vendor/ncac/phpcs-standard/.php-cs-fixer.dist.php

# Step 2: Apply PHPCBF for NCAC-specific corrections
vendor/bin/phpcbf --standard=NCAC src/

# Step 3: Check remaining violations
vendor/bin/phpcs --standard=NCAC src/
bash
# 1. Complex transformations via PHP-CS-Fixer
php-cs-fixer fix --config=.ncac-cs-fixer.php src/

# 2. Simple fixes + validation via PHPCS  
vendor/bin/phpcs --standard=NCAC src/ --fix

# 3. Final validation
vendor/bin/phpcs --standard=NCAC src/