Skip to content

Commit

Permalink
Add PHP_CodeSniffer and fix low-hanging fruit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelle Raaijmakers committed Feb 25, 2019
1 parent 0494494 commit b495f17
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 40 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"composer-plugin-api": "^1.1.0"
},
"require-dev": {
"composer/composer": "^1.8.4"
"composer/composer": "^1.8.4",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5",
"object-calisthenics/phpcs-calisthenics-rules": "^3.4",
"slevomat/coding-standard": "^4.8"
},
"autoload": {
"psr-4": {
Expand Down
119 changes: 119 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="composer-velocita">
<description>PHP_CodeSniffer ruleset for composer-velocita</description>

<!-- Include src folder -->
<file>src</file>

<!-- Arguments -->
<arg value="sp"/>
<arg name="colors"/>

<!-- PSR2 standard -->
<rule ref="PSR2"/>

<!-- Extra rules -->
<rule ref="Generic.Arrays.ArrayIndent"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.PHP.ForbiddenFunctions">
<properties>
<property name="forbiddenFunctions" type="array" value="empty=>null,isset=>null,is_null=>null"/>
</properties>
</rule>
<rule ref="Generic.PHP.Syntax"/>
<rule ref="Generic.Classes.DuplicateClassName"/>
<rule ref="MySource.PHP.GetRequestData"/>
<rule ref="Generic.Commenting.Fixme"/>
<rule ref="Generic.Commenting.Todo"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop"/>
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
<rule ref="Generic.Metrics.CyclomaticComplexity">
<properties>
<property name="complexity" value="10"/>
<property name="absoluteComplexity" value="20"/>
</properties>
</rule>
<rule ref="PSR12.Operators.OperatorSpacing"/>

<!-- 3rd party -->
<rule ref="ObjectCalisthenics.Files.FunctionLength">
<properties>
<property name="maxLength" value="25"/>
</properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.MaxNestingLevel">
<properties>
<property name="maxNestingLevel" value="3"/>
</properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.MethodPerClassLimit">
<properties>
<property name="maxCount" value="10"/>
</properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.PropertyPerClassLimit">
<properties>
<property name="maxCount" value="10"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements"/>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/>
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFullyQualifiedGlobalFunctions" value="true"/>
<property name="allowFullyQualifiedGlobalConstants" value="true"/>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace"/>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="2"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingPropertyTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<rule ref="SlevomatCodingStandard.TypeHints.UselessConstantTypeHint"/>
<rule ref="SlevomatCodingStandard.Types.EmptyLinesAroundTypeBraces">
<properties>
<property name="linesCountAfterOpeningBrace" value="0"/>
<property name="linesCountBeforeClosingBrace" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator"/>
<rule ref="SlevomatCodingStandard.PHP.TypeCast"/>
<rule ref="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming"/>
<rule ref="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"/>
<rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>
<rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
<properties>
<property name="spacing" value="1"/>
<property name="spacingBeforeFirst" value="0"/>
<property name="spacingAfterLast" value="0"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false"/>
</properties>
</rule>
</ruleset>
2 changes: 2 additions & 0 deletions src/Commands/CommandProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Commands;

use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/DisableCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Commands;

use Composer\Command\BaseCommand;
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/EnableCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Commands;

use Composer\Command\BaseCommand;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/EndpointMapping.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Config;

class EndpointMapping
Expand Down
6 changes: 4 additions & 2 deletions src/Config/Endpoints.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Config;

class Endpoints
Expand Down Expand Up @@ -27,12 +29,12 @@ public static function fromArray(array $data): Endpoints
return $endpoints;
}

public function addRepository(EndpointMapping $mapping)
public function addRepository(EndpointMapping $mapping): void
{
$this->repositories[] = $mapping;
}

public function addDistributionChannel(EndpointMapping $mapping)
public function addDistributionChannel(EndpointMapping $mapping): void
{
$this->distributionChannels[] = $mapping;
}
Expand Down
10 changes: 7 additions & 3 deletions src/Config/PluginConfig.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Config;

use Exception;

class PluginConfig
{
/** @var bool */
Expand Down Expand Up @@ -49,13 +53,13 @@ public function setURL(string $url): void
public function validate(): void
{
// If set, the URL must be valid
if (($this->url !== null) && !filter_var($this->url, FILTER_VALIDATE_URL)) {
throw new \Exception('Invalid URL was set for this plugin');
if (($this->url !== null) && !\filter_var($this->url, \FILTER_VALIDATE_URL)) {
throw new Exception('Invalid URL was set for this plugin');
}

// If enabled, a URL must also be set
if ($this->enabled && ($this->url === null)) {
throw new \Exception('A URL must be set for this plugin to be enabled');
throw new Exception('A URL must be set for this plugin to be enabled');
}
}
}
6 changes: 5 additions & 1 deletion src/Exceptions/IOException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Exceptions;

class IOException extends \RuntimeException
use RuntimeException;

class IOException extends RuntimeException
{
}
33 changes: 18 additions & 15 deletions src/Plugins/VelocitaPlugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Plugins;

use Composer\Composer;
Expand All @@ -9,6 +11,7 @@
use Composer\Plugin\Capable;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PreFileDownloadEvent;
use Exception;
use ISAAC\Velocita\Composer\Commands\CommandProvider;
use ISAAC\Velocita\Composer\Config\Endpoints;
use ISAAC\Velocita\Composer\Config\PluginConfig;
Expand Down Expand Up @@ -40,7 +43,7 @@ public function activate(Composer $composer, IOInterface $io): void
$this->composer = $composer;
$this->io = $io;

$this->configPath = sprintf('%s/%s', ComposerFactory::getHomeDir(), self::CONFIG_FILE);
$this->configPath = \sprintf('%s/%s', ComposerFactory::getHomeDir(), self::CONFIG_FILE);
}

public function getCapabilities(): array
Expand Down Expand Up @@ -73,9 +76,9 @@ public function onPreFileDownload(PreFileDownloadEvent $event): void
*/
try {
$this->handlePreFileDownloadEvent($event);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->io->writeError(
sprintf(
\sprintf(
"<error>Velocita: exception thrown in event handler: %s\n%s</error>",
$e->getMessage(),
$e->getTraceAsString()
Expand Down Expand Up @@ -105,10 +108,10 @@ protected function handlePreFileDownloadEvent(PreFileDownloadEvent $event): void
protected function loadConfiguration(): PluginConfig
{
$data = null;
if (is_readable($this->configPath)) {
$data = json_decode(file_get_contents($this->configPath), true);
if (\is_readable($this->configPath)) {
$data = \json_decode(\file_get_contents($this->configPath), true);
}
if (!is_array($data)) {
if (!\is_array($data)) {
$data = [];
}
return PluginConfig::fromArray($data);
Expand All @@ -127,26 +130,26 @@ public function writeConfiguration(PluginConfig $config): void
$config->validate();

// Ensure parent directory exists
$configDir = dirname($this->configPath);
if (!is_dir($configDir)) {
mkdir($configDir, 0777, true);
$configDir = \dirname($this->configPath);
if (!\is_dir($configDir)) {
\mkdir($configDir, 0777, true);
}

file_put_contents($this->configPath, json_encode($config->toArray()));
\file_put_contents($this->configPath, \json_encode($config->toArray()));
}

protected function loadEndpoints(): Endpoints
{
$config = $this->getConfiguration();
$endpointsURL = sprintf('%s/endpoints', $config->getURL());
$endpointsJSON = file_get_contents($endpointsURL);
$endpointsURL = \sprintf('%s/endpoints', $config->getURL());
$endpointsJSON = \file_get_contents($endpointsURL);
if ($endpointsJSON === false) {
throw new IOException('Unable to retrieve endpoints configuration from Velocita');
}
$endpoints = json_decode($endpointsJSON, true);
if (!is_array($endpoints)) {
$endpoints = \json_decode($endpointsJSON, true);
if (!\is_array($endpoints)) {
throw new IOException(
sprintf('Invalid JSON structure retrieved (#%d: %s)', json_last_error(), json_last_error_msg())
\sprintf('Invalid JSON structure retrieved (#%d: %s)', \json_last_error(), \json_last_error_msg())
);
}
return Endpoints::fromArray($endpoints);
Expand Down
3 changes: 3 additions & 0 deletions src/Plugins/VelocitaPluginInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Plugins;

use Composer\Plugin\PluginInterface;
Expand All @@ -9,5 +11,6 @@
interface VelocitaPluginInterface extends PluginInterface
{
public function getConfiguration(): PluginConfig;

public function getEndpoints(): Endpoints;
}
2 changes: 2 additions & 0 deletions src/Util/ComposerFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ISAAC\Velocita\Composer\Util;

use Composer\Factory;
Expand Down
Loading

0 comments on commit b495f17

Please sign in to comment.