How can PHP CodeSniffer check files that have no extension?

Viewed 2098

In my team we are using codesniffer to enforce a coding style for a Symfony application, and just realized that the files without extension are not getting checked, even if we explicitly use the file as an argument. This is by design as discussed on this github issue.

That means that files like bin/console are not getting checked, even though they are valid PHP files, and the --extensions argument doesn't accept and empty argument.

Is there a way to make CodeSniffer check these files as well?

3 Answers

From the docs

The ignoring of file extensions for specific files is a feature of PHP_CodeSniffer and is the only way to check files without an extension. If you check an entire directory of files, all files without extensions will be ignored, so you must check each of these file separately

https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage

So you will need to specify the file

The syntax is

phpcs --file-list=path/to/file/containing/others

Where others is a file with a file per line structure.

This check all files listed in the others file

Eg

File1 File2 Console

To check the 3 files

I think you can specify * as wildcard extension.

If that does not work it states in the docs:

If you have asked PHP_CodeSniffer to check a specific file rather than an entire directory, the extension of the specified file will be ignored. The file will be checked even if it has an invalid extension or no extension at all. In the following example, the main.inc file will be checked by PHP_CodeSniffer even though the --extensions command line argument specifies that only .php files should be checked. Extension ignored when checking specific file

$ phpcs --extensions=php /path/to/code/main.inc

So it should work if you explicitly check a single file that has no extension.

Further it states:

The ignoring of file extensions for specific files is a feature of PHP_CodeSniffer and is the only way to check files without an extension. If you check an entire directory of files, all files without extensions will be ignored, so you must check each of these file separately.

See https://pear.php.net/manual/en/package.php.php-codesniffer.advanced-usage.php

I don't know if CS can do this, but I think files like bin/console are mostly up to ten lines, so there is no need to check them with codesniffer :) They should just require vendor and run some Application class from source. Also they are all same in all projects.

Just for reference, our bin/console.

#!/usr/bin/env php
<?php declare(strict_types = 1);

/** @var \Nette\DI\Container $container */
$container = require __DIR__ . '/../src/bootstrap.php';

/** @var \Symfony\Component\Console\Application $console */
$console = $container->getByType(Symfony\Component\Console\Application::class);

$services = $container->findByType(Symfony\Component\Console\Command\Command::class);

foreach ($services as $service) {
    $console->add(
        $container->getService($service)
    );
}

exit($console->run());

There is no reason to check this.

Related