Skip to content

Commit

Permalink
Apply CodeSniffer (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 28, 2018
1 parent ec1541a commit 188ff49
Show file tree
Hide file tree
Showing 31 changed files with 123 additions and 113 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
/vendor/
/vendor-bin/*/vendor/
/vendor-bin/*/bin/
/.travis/
!/.travis/.gitkeep
!/.travis/secrets.tar.enc
/.phpcs-cache
/phpcs.xml
Empty file removed .travis/.gitkeep
Empty file.
Binary file removed .travis/secrets.tar.enc
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CODE_SNIFFER_FIX=vendor-bin/code-sniffer/vendor/bin/phpcbf
cs: ## Fixes CS
cs: $(CODE_SNIFFER) $(CODE_SNIFFER_FIX)
$(PHPNOGC) $(CODE_SNIFFER_FIX) || true
$(PHPNOGC) $(PHP_CS_FIXER) fix
$(PHPNOGC) $(CODE_SNIFFER)

.PHONY: build
build: ## Build the PHAR
Expand Down
4 changes: 0 additions & 4 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<arg name="colors"/>
<arg value="ps"/>

<file>fixtures</file>
<exclude-pattern>default_stub\.php</exclude-pattern>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/build/</exclude-pattern>
<file>src</file>
<file>tests</file>

Expand Down
6 changes: 3 additions & 3 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function dump(string $prefix): string
private function createClassAliasStatements(array $whitelistedClasses, bool $hasNamespacedFunctions): array
{
$statements = array_map(
function (string $prefixedClass): string {
static function (string $prefixedClass): string {
return sprintf(
'class_exists(\'%s\');',
$prefixedClass
Expand All @@ -118,7 +118,7 @@ function (string $prefixedClass): string {

if ($hasNamespacedFunctions) {
$statements = array_map(
function (string $statement): string {
static function (string $statement): string {
return str_repeat(' ', 4).$statement;
},
$statements
Expand All @@ -145,7 +145,7 @@ function (string $statement): string {
private function createFunctionAliasStatements(array $whitelistedFunctions, bool $hasNamespacedFunctions): array
{
$statements = array_map(
function (array $node) use ($hasNamespacedFunctions): string {
static function (array $node) use ($hasNamespacedFunctions): string {
/**
* @var string
* @var string $alias
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private static function retrieveFilesFromPaths(array $paths): iterable
$finder->files()
->in($pathsToSearch)
->append($filesToAppend)
->filter(function (SplFileInfo $fileInfo): ?bool {
->filter(static function (SplFileInfo $fileInfo): ?bool {
if ($fileInfo->isLink()) {
return false;
}
Expand All @@ -521,7 +521,7 @@ private static function retrieveFilesWithContents(Iterator $files): array
{
return array_reduce(
iterator_to_array($files),
function (array $files, SplFileInfo $fileInfo): array {
static function (array $files, SplFileInfo $fileInfo): array {
$file = $fileInfo->getRealPath();

if (false === $file) {
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private function scopeFiles(

usort(
$vendorDirs,
function ($a, $b) {
static function ($a, $b) {
return strlen($b) <=> strlen($a);
}
);
Expand Down Expand Up @@ -287,7 +287,7 @@ private function validatePaths(InputInterface $input): void
$fileSystem = $this->fileSystem;

$paths = array_map(
function (string $path) use ($cwd, $fileSystem) {
static function (string $path) use ($cwd, $fileSystem) {
if (false === $fileSystem->isAbsolutePath($path)) {
return $cwd.DIRECTORY_SEPARATOR.$path;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpParser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function replaceGroupUseStatements(array $nodes): array
private function createUses_(GroupUse $node): array
{
return array_map(
function (UseUse $use) use ($node): Use_ {
static function (UseUse $use) use ($node): Use_ {
$newUse = new UseUse(
Name::concat($node->prefix, $use->name, $use->name->getAttributes()),
$use->alias,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class NamespaceStmtCollection implements IteratorAggregate, Countable
* @param Namespace_ $node New namespace, may have been prefixed.
* @param Namespace_ $originalName Original unchanged namespace.
*/
public function add(Namespace_ $node, Namespace_ $originalName)
public function add(Namespace_ $node, Namespace_ $originalName): void
{
$this->nodes[] = $originalName;

Expand Down
15 changes: 11 additions & 4 deletions src/Scoper/Composer/AutoloadPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Humbug\PhpScoper\Whitelist;
use stdClass;
use function array_map;
use function is_array;

/**
* @private
Expand Down Expand Up @@ -104,11 +105,16 @@ private static function mergePSR0And4(array $psr0, array $psr4): array
return $psr4;
}

private static function updatePSR0Path($path, $namespace)
/**
* @param string|string[] $path
*
* @return string|string[]
*/
private static function updatePSR0Path($path, string $namespace)
{
$namespaceForPsr = str_replace('\\', '/', $namespace);

if (!is_array($path)) {
if (false === is_array($path)) {
if ('/' !== substr($path, -1)) {
$path .= '/';
}
Expand All @@ -117,6 +123,7 @@ private static function updatePSR0Path($path, $namespace)

return $path;
}

foreach ($path as $key => $item) {
if ('/' !== substr($item, -1)) {
$item .= '/';
Expand All @@ -134,7 +141,7 @@ private static function updatePSR0Path($path, $namespace)
* PSR0 | PSR4
* array |
* string |
* or simply the namepace not existing as a psr-4 entry.
* or simply the namespace not existing as a psr-4 entry.
*
* @param string $psr0Namespace
* @param string|array $psr0Path
Expand Down Expand Up @@ -172,7 +179,7 @@ private static function mergeNamespaces(string $psr0Namespace, $psr0Path, $psr4)
private static function prefixLaravelProviders(array $providers, string $prefix, Whitelist $whitelist): array
{
return array_map(
function (string $provider) use ($prefix, $whitelist): string {
static function (string $provider) use ($prefix, $whitelist): string {
return $whitelist->belongsToWhitelistedNamespace($provider)
? $provider
: sprintf('%s\\%s', $prefix, $provider)
Expand Down
2 changes: 1 addition & 1 deletion src/Scoper/PatchScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function scope(string $filePath, string $contents, string $prefix, array

return array_reduce(
$patchers,
function (string $contents, callable $patcher) use ($filePath, $prefix): string {
static function (string $contents, callable $patcher) use ($filePath, $prefix): string {
return $patcher($filePath, $prefix, $contents);
},
$contents
Expand Down
4 changes: 3 additions & 1 deletion src/Throwable/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Humbug\PhpScoper\Throwable\Exception;

class RuntimeException extends \RuntimeException
use RuntimeException as RootRuntimeException;

class RuntimeException extends RootRuntimeException
{
}
4 changes: 2 additions & 2 deletions src/Whitelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static function create(
private static function assertValidPattern(string $element): void
{
if (1 !== preg_match('/^(([\p{L}_]+\\\\)+)?[\p{L}_]*\*$/u', $element)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
sprintf(
'Invalid whitelist pattern "%s".',
$element
Expand Down Expand Up @@ -272,7 +272,7 @@ public function getClassWhitelistArray(): array
{
return array_filter(
$this->original,
function (string $name): bool {
static function (string $name): bool {
return '*' !== $name && '\*' !== substr($name, -2);
}
);
Expand Down
19 changes: 10 additions & 9 deletions tests/Autoload/ScoperAutoloadGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Humbug\PhpScoper\Autoload;

use Generator;
use Humbug\PhpScoper\Whitelist;
use PhpParser\Node\Name\FullyQualified;
use PHPUnit\Framework\TestCase;
Expand All @@ -23,7 +24,7 @@ class ScoperAutoloadGeneratorTest extends TestCase
/**
* @dataProvider provideWhitelists
*/
public function test_generate_the_autoload(Whitelist $whitelist, string $expected)
public function test_generate_the_autoload(Whitelist $whitelist, string $expected): void
{
$prefix = 'Humbug';

Expand All @@ -34,7 +35,7 @@ public function test_generate_the_autoload(Whitelist $whitelist, string $expecte
$this->assertSame($expected, $actual);
}

public function provideWhitelists()
public function provideWhitelists(): Generator
{
yield 'empty whitelist' => [
Whitelist::create(true, true, true),
Expand All @@ -51,7 +52,7 @@ public function provideWhitelists()
];

yield 'whitelist with whitelisted global functions recorded' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true);

$whitelist->recordWhitelistedFunction(
Expand Down Expand Up @@ -92,7 +93,7 @@ function bar() {
];

yield 'whitelist with whitelisted namespaced functions recorded' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true);

$whitelist->recordWhitelistedFunction(
Expand Down Expand Up @@ -167,7 +168,7 @@ function baz() {
];

yield 'whitelist with whitelisted classes recorded' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true, 'A\Foo', 'B\Bar');

$whitelist->recordWhitelistedClass(
Expand All @@ -194,7 +195,7 @@ class_exists('Humbug\A\Foo');
];

yield 'whitelist with whitelisted global classes recorded' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true);

$whitelist->recordWhitelistedClass(
Expand Down Expand Up @@ -227,7 +228,7 @@ class_exists('Humbug\Bar');
];

yield 'complete whitelist' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true, 'A\Foo', 'B\Bar');

$whitelist->recordWhitelistedClass(
Expand Down Expand Up @@ -324,7 +325,7 @@ function baz() {

// https://github.com/humbug/php-scoper/issues/267
yield '__autoload global function with no namespaced functions' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true);

$whitelist->recordWhitelistedFunction(
Expand Down Expand Up @@ -356,7 +357,7 @@ function __autoload($className) {

// https://github.com/humbug/php-scoper/issues/267
yield '__autoload global function with namespaced functions' => [
(function () {
(static function () {
$whitelist = Whitelist::create(true, true, true);

$whitelist->recordWhitelistedFunction(
Expand Down
16 changes: 8 additions & 8 deletions tests/Console/Command/AddPrefixCommandIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function setUp(): void
file_put_contents('scoper.inc.php', '<?php return [];');
}

public function test_scope_the_given_paths()
public function test_scope_the_given_paths(): void
{
$input = [
'add-prefix',
Expand All @@ -71,7 +71,7 @@ public function test_scope_the_given_paths()
$this->assertFilesAreSame(self::FIXTURE_PATH.'/../scoped', $this->tmp);
}

public function test_scope_in_quiet_mode()
public function test_scope_in_quiet_mode(): void
{
$input = [
'add-prefix',
Expand All @@ -93,7 +93,7 @@ public function test_scope_in_quiet_mode()
$this->assertSame(0, $this->appTester->getStatusCode());
}

public function test_scope_in_normal_mode()
public function test_scope_in_normal_mode(): void
{
$input = [
'add-prefix',
Expand Down Expand Up @@ -138,7 +138,7 @@ public function test_scope_in_normal_mode()
$this->assertSame(0, $this->appTester->getStatusCode());
}

public function test_scope_in_verbose_mode()
public function test_scope_in_verbose_mode(): void
{
$input = [
'add-prefix',
Expand Down Expand Up @@ -184,7 +184,7 @@ public function test_scope_in_verbose_mode()
$this->assertSame(0, $this->appTester->getStatusCode());
}

public function test_scope_in_very_verbose_mode()
public function test_scope_in_very_verbose_mode(): void
{
$input = [
'add-prefix',
Expand Down Expand Up @@ -258,7 +258,7 @@ public function test_scope_in_very_verbose_mode()
$this->assertSame(0, $this->appTester->getStatusCode());
}

private function getNormalizeDisplay(string $display)
private function getNormalizeDisplay(string $display): string
{
$display = str_replace(realpath(self::FIXTURE_PATH), '/path/to', $display);
$display = str_replace($this->tmp, '/path/to', $display);
Expand All @@ -283,7 +283,7 @@ private function getNormalizeDisplay(string $display)
return implode("\n", $lines);
}

private function assertFilesAreSame(string $expectedDir, string $actualDir)
private function assertFilesAreSame(string $expectedDir, string $actualDir): void
{
$expected = $this->collectFiles($expectedDir);

Expand All @@ -304,7 +304,7 @@ private function collectFiles(string $dir): array

return array_reduce(
iterator_to_array($files),
function (array $collectedFiles, SplFileInfo $file) use ($dir): array {
static function (array $collectedFiles, SplFileInfo $file) use ($dir): array {
$path = str_replace($dir, '', $file->getRealPath());

$collectedFiles[$path] = file_get_contents($file->getRealPath());
Expand Down
Loading

0 comments on commit 188ff49

Please sign in to comment.