Skip to content

Commit

Permalink
Merge pull request doctrine#230 from lcobucci/add-phpcs
Browse files Browse the repository at this point in the history
Add PHPCS to the project
  • Loading branch information
lcobucci authored Aug 25, 2017
2 parents 988ea5e + 3557447 commit 2121298
Show file tree
Hide file tree
Showing 29 changed files with 122 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
build.properties export-ignore
build.xml export-ignore
phpunit.xml.dist export-ignore
phpcs.xml.dist export-ignore
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ vendor/
build/
phpunit.xml
composer.lock
clover.xml
clover.xml
.phpcs-cache
phpcs.xml
37 changes: 24 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
language: php

dist: trusty
sudo: false
language: php

cache:
directories:
- $HOME/.composer/cache

php:
- 7.1
- 7.2
- nightly

services:
Expand All @@ -20,19 +21,29 @@ before_install:
- pecl channel-update pecl.php.net
- pecl config-set preferred_state beta
- echo yes | pecl upgrade apcu
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
- phpenv config-add ./tests/travis/php.ini
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then PHPUNIT_FLAGS="--coverage-clover clover.xml"; fi

install:
- travis_retry composer install

script:
- ./vendor/bin/phpunit --exclude-group performance $PHPUNIT_FLAGS

after_script:
- if [[ -f clover.xml ]]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover clover.xml; fi

matrix:
fast_finish: true
allow_failures:
- php: nightly
script: ./vendor/bin/phpunit --exclude-group performance

jobs:
allow_failures:
- php: nightly

include:
- stage: Coding standard
script:
- ./vendor/bin/phpcs

- stage: Coverage
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
script:
- ./vendor/bin/phpunit --exclude-group performance --coverage-clover clover.xml
after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover clover.xml
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"alcaeus/mongo-php-adapter": "^1.1",
"mongodb/mongodb": "^1.1",
"phpunit/phpunit": "^5.7",
"predis/predis": "~1.0"
"predis/predis": "~1.0",
"doctrine/coding-standard": "^1.0"
},
"suggest": {
"alcaeus/mongo-php-adapter": "Required to use legacy MongoDB driver"
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Cache/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct()
*/
protected function doFetch($id)
{
if (! $this->doContains($id)) {
if ( ! $this->doContains($id)) {
$this->missesCount += 1;

return false;
Expand All @@ -81,7 +81,7 @@ protected function doFetch($id)
*/
protected function doContains($id)
{
if (! isset($this->data[$id])) {
if ( ! isset($this->data[$id])) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/Common/Cache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function deleteAll()
*/
private function getNamespacedId(string $id) : string
{
$namespaceVersion = $this->getNamespaceVersion();
$namespaceVersion = $this->getNamespaceVersion();

return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
}
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
$success = true;

foreach ($keysAndValues as $key => $value) {
if (!$this->doSave($key, $value, $lifetime)) {
if ( ! $this->doSave($key, $value, $lifetime)) {
$success = false;
}
}
Expand Down Expand Up @@ -306,7 +306,7 @@ protected function doDeleteMultiple(array $keys)
$success = true;

foreach ($keys as $key) {
if (! $this->doDelete($key)) {
if ( ! $this->doDelete($key)) {
$success = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Cache/ChainCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function doFetch($id)
$value = $cacheProvider->doFetch($id);

// We populate all the previous cache layers (that are assumed to be faster)
for ($subKey = $key - 1 ; $subKey >= 0 ; $subKey--) {
for ($subKey = $key - 1; $subKey >= 0; $subKey--) {
$this->cacheProviders[$subKey]->doSave($id, $value);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/Common/Cache/ExtMongoDBCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(Collection $collection)
{
// Ensure there is no typemap set - we want to use our own
$this->collection = $collection->withOptions(['typeMap' => null]);
$this->database = new Database($collection->getManager(), $collection->getDatabaseName());
$this->database = new Database($collection->getManager(), $collection->getDatabaseName());
}

/**
Expand Down Expand Up @@ -166,7 +166,7 @@ protected function doFlush()
*/
protected function doGetStats()
{
$uptime = null;
$uptime = null;
$memoryUsage = null;

try {
Expand All @@ -177,12 +177,12 @@ protected function doGetStats()
'recordStats' => 0,
'repl' => 0,
])->toArray()[0];
$uptime = $serverStatus['uptime'] ?? null;
$uptime = $serverStatus['uptime'] ?? null;
} catch (Exception $e) {
}

try {
$collStats = $this->database->command(['collStats' => $this->collection->getCollectionName()])->toArray()[0];
$collStats = $this->database->command(['collStats' => $this->collection->getCollectionName()])->toArray()[0];
$memoryUsage = $collStats['size'] ?? null;
} catch (Exception $e) {
}
Expand All @@ -203,14 +203,14 @@ protected function doGetStats()
*
* @return bool
*/
private function isExpired(BSONDocument $document): bool
private function isExpired(BSONDocument $document) : bool
{
return isset($document[MongoDBCache::EXPIRATION_FIELD]) &&
$document[MongoDBCache::EXPIRATION_FIELD] instanceof UTCDateTime &&
$document[MongoDBCache::EXPIRATION_FIELD]->toDateTime() < new \DateTime();
}

private function createExpirationIndex(): void
private function createExpirationIndex() : void
{
if ($this->expirationIndexCreated) {
return;
Expand Down
7 changes: 3 additions & 4 deletions lib/Doctrine/Common/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ protected function getFilename($id)
$hash = hash('sha256', $id);

// This ensures that the filename is unique and that there are no invalid chars in it.
if (
'' === $id
if ('' === $id
|| ((strlen($id) * 2 + $this->extensionStringLength) > 255)
|| ($this->isRunningOnWindows && ($this->directoryStringLength + 4 + strlen($id) * 2 + $this->extensionStringLength) > 258)
) {
Expand Down Expand Up @@ -195,7 +194,7 @@ protected function doGetStats()
{
$usage = 0;
foreach ($this->getIterator() as $name => $file) {
if (! $file->isDir() && $this->isFilenameEndingWithExtension($name)) {
if ( ! $file->isDir() && $this->isFilenameEndingWithExtension($name)) {
$usage += $file->getSize();
}
}
Expand All @@ -220,7 +219,7 @@ protected function doGetStats()
private function createPathIfNeeded(string $path) : bool
{
if ( ! is_dir($path)) {
if (false === @mkdir($path, 0777 & (~$this->umask), true) && !is_dir($path)) {
if (false === @mkdir($path, 0777 & (~$this->umask), true) && ! is_dir($path)) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ protected function doSave($id, $data, $lifeTime = 0)
$lifeTime = time() + $lifeTime;
}

$data = serialize($data);
$filename = $this->getFilename($id);
$data = serialize($data);
$filename = $this->getFilename($id);

return $this->writeFile($filename, $lifeTime . PHP_EOL . $data);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Cache/LegacyMongoDBCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private function isExpired(array $document) : bool
}


private function createExpirationIndex(): void
private function createExpirationIndex() : void
{
if ($this->expirationIndexCreated) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Cache/MultiDeleteCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ interface MultiDeleteCache
*
* @return bool TRUE if the operation was successful, FALSE if it wasn't.
*/
function deleteMultiple(array $keys);
public function deleteMultiple(array $keys);
}
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Cache/MultiGetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ interface MultiGetCache
* @return mixed[] Array of retrieved values, indexed by the specified keys.
* Values that couldn't be retrieved are not contained in this array.
*/
function fetchMultiple(array $keys);
public function fetchMultiple(array $keys);
}
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Cache/MultiPutCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ interface MultiPutCache
*
* @return bool TRUE if the operation was successful, FALSE if it wasn't.
*/
function saveMultiple(array $keysAndValues, $lifetime = 0);
public function saveMultiple(array $keysAndValues, $lifetime = 0);
}
12 changes: 6 additions & 6 deletions lib/Doctrine/Common/Cache/PhpFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ protected function doSave($id, $data, $lifeTime = 0)
$lifeTime = time() + $lifeTime;
}

$filename = $this->getFilename($id);
$filename = $this->getFilename($id);

$value = [
'lifetime' => $lifeTime,
'data' => $data
];

if (is_object($data) && method_exists($data, '__set_state')) {
$value = var_export($value, true);
$code = sprintf('<?php return %s;', $value);
$value = var_export($value, true);
$code = sprintf('<?php return %s;', $value);
} else {
$value = var_export(serialize($value), true);
$code = sprintf('<?php return unserialize(%s);', $value);
$value = var_export(serialize($value), true);
$code = sprintf('<?php return unserialize(%s);', $value);
}

return $this->writeFile($filename, $code);
Expand All @@ -122,7 +122,7 @@ private function includeFileForId(string $id) : ?array

restore_error_handler();

if (! isset($value['lifetime'])) {
if ( ! isset($value['lifetime'])) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Cache/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function doFetchMultiple(array $keys)
$fetchedItems = array_combine($keys, $this->redis->mget($keys));

// Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data.
$foundItems = [];
$foundItems = [];

foreach ($fetchedItems as $key => $value) {
if (false !== $value || $this->redis->exists($key)) {
Expand All @@ -95,7 +95,7 @@ protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)

// Keys have lifetime, use SETEX for each of them
foreach ($keysAndValues as $key => $value) {
if (!$this->redis->setex($key, $lifetime, $value)) {
if ( ! $this->redis->setex($key, $lifetime, $value)) {
$success = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Cache/SQLite3Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function doFetch($id)
{
$item = $this->findById($id);

if (!$item) {
if ( ! $item) {
return false;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ private function findById($id, bool $includeData = true) : ?array
{
list($idField) = $fields = $this->getFields();

if (!$includeData) {
if ( ! $includeData) {
$key = array_search(static::DATA_FIELD, $fields);
unset($fields[$key]);
}
Expand Down
22 changes: 22 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<ruleset name="Coding Standards for doctrine">
<description>Coding Standards for doctrine.</description>

<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors" />

<!-- Ignore warnings and show progress of the run -->
<arg value="np"/>

<file>lib</file>
<file>tests</file>

<rule ref="Doctrine"/>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
</ruleset>
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Common/Cache/ApcCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApcCacheTest extends CacheTest
{
protected function setUp() : void
{
if (!ini_get('apc.enable_cli')) {
if ( ! ini_get('apc.enable_cli')) {
$this->markTestSkipped('APC must be enabled for the CLI with the ini setting apc.enable_cli=1');
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Common/Cache/ApcuCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApcuCacheTest extends CacheTest
{
protected function setUp() : void
{
if (!ini_get('apc.enable_cli')) {
if ( ! ini_get('apc.enable_cli')) {
$this->markTestSkipped('APC must be enabled for the CLI with the ini setting apc.enable_cli=1');
}
}
Expand Down
Loading

0 comments on commit 2121298

Please sign in to comment.