Skip to content

Commit

Permalink
Correct style and PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sstok committed Aug 19, 2017
1 parent 12e17bc commit ea37af9
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 186 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ matrix:
- php: 7.0
env: DEPENDENCIES='dev'
- php: 7.1
env: DEPENDENCIES='low'
env: DEPENDENCIES='low' lint=1
fast_finish: true

sudo: false
Expand All @@ -17,6 +17,10 @@ cache:

before_install:
- phpenv config-rm xdebug.ini || echo "xdebug not available"
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- if [[ $lint = 1 ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.3.2/php-cs-fixer.phar; fi
- if [[ $lint = 1 ]]; then composer global require --dev 'phpstan/phpstan:^0.8'; fi
- export PATH="$PATH:$HOME/.composer/vendor/bin"

install:
- if [ "$DEPENDENCIES" == "dev" ]; then composer config minimum-stability dev; fi;
Expand All @@ -28,3 +32,5 @@ script:
- export SYMFONY_PHPUNIT_REMOVE="symfony/yaml"
- export SYMFONY_DEPRECATIONS_HELPER=strict
- vendor/bin/simple-phpunit --verbose
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
- if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
autoload_files:
- vendor/autoload.php
- vendor/symfony/phpunit-bridge/bin/.phpunit/phpunit-5.7.1/vendor/autoload.php

ignoreErrors:
#- '#__construct\(\) does not call parent constructor from .+#'
- '#Access to an undefined property Symfony\\Component\\Validator\\Constraint\:\:#'
- "#Casting to string something that's already string#"

# Tests
- '#Parameter \#1 \$password of method Rollerworks\\Component\\PasswordStrength\\Blacklist\\[a-zA-Z]+\:\:isBlacklisted\(\) expects string#'
2 changes: 1 addition & 1 deletion src/Blacklist/ArrayProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class ArrayProvider implements BlacklistProviderInterface
{
private $blacklist = array();
private $blacklist = [];

/**
* @param array $blacklist
Expand Down
2 changes: 1 addition & 1 deletion src/Blacklist/ChainProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ChainProvider implements BlacklistProviderInterface
*
* @param BlacklistProviderInterface[] $providers
*/
public function __construct(array $providers = array())
public function __construct(array $providers = [])
{
foreach ($providers as $provider) {
$this->addProvider($provider);
Expand Down
14 changes: 7 additions & 7 deletions src/Blacklist/PdoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public function add($password)
}

$db = $this->initDb();
$args = array(
$args = [
':password' => $password,
':created_at' => time(),
);
];

try {
if ($this->isBlacklisted($password)) {
Expand Down Expand Up @@ -84,9 +84,9 @@ public function delete($password)
}

$db = $this->initDb();
$args = array(
$args = [
':password' => $password,
);
];

try {
$this->exec($db, 'DELETE FROM rollerworks_passdbl WHERE passwd = :password', $args);
Expand Down Expand Up @@ -132,7 +132,7 @@ public function isBlacklisted($password)
}

$db = $this->initDb();
$tokenExists = $this->fetch($db, 'SELECT 1 FROM rollerworks_passdbl WHERE passwd = :password LIMIT 1', array(':password' => $password));
$tokenExists = $this->fetch($db, 'SELECT 1 FROM rollerworks_passdbl WHERE passwd = :password LIMIT 1', [':password' => $password]);

return !empty($tokenExists);
}
Expand All @@ -151,7 +151,7 @@ abstract protected function initDb();
*
* @return mixed
*/
protected function fetch($db, $query, array $args = array())
protected function fetch($db, $query, array $args = [])
{
$stmt = $this->prepareStatement($db, $query);

Expand All @@ -170,7 +170,7 @@ protected function fetch($db, $query, array $args = array())
*
* @throws \RuntimeException
*/
protected function exec($db, $query, array $args = array())
protected function exec($db, $query, array $args = [])
{
$stmt = $this->prepareStatement($db, $query);

Expand Down
8 changes: 4 additions & 4 deletions src/Blacklist/SqliteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function initDb()
/**
* {@inheritdoc}
*/
protected function exec($db, $query, array $args = array())
protected function exec($db, $query, array $args = [])
{
if ($db instanceof \SQLite3) {
$stmt = $this->prepareStatement($db, $query);
Expand All @@ -98,12 +98,12 @@ protected function exec($db, $query, array $args = array())
/**
* {@inheritdoc}
*/
protected function fetch($db, $query, array $args = array())
protected function fetch($db, $query, array $args = [])
{
$return = array();
$return = [];

if ($db instanceof \SQLite3) {
$stmt = $this->prepareStatement($db, $query, true);
$stmt = $this->prepareStatement($db, $query);
foreach ($args as $arg => $val) {
$stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
}
Expand Down
1 change: 0 additions & 1 deletion src/Command/BlacklistCommonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Rollerworks\Component\PasswordStrength\Command;

use Rollerworks\Component\PasswordStrength\Blacklist\SqliteProvider;
use Rollerworks\Component\PasswordStrength\Blacklist\UpdatableBlacklistProviderInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Command/BlacklistDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Rollerworks\Component\PasswordStrength\Command;

use Rollerworks\Component\PasswordStrength\Blacklist\SqliteProvider;
use Rollerworks\Component\PasswordStrength\Blacklist\UpdatableBlacklistProviderInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down
3 changes: 0 additions & 3 deletions src/Command/BlacklistPurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

namespace Rollerworks\Component\PasswordStrength\Command;

use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;

class BlacklistPurgeCommand extends BlacklistCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function validate($value, Constraint $constraint)

if ($constraint->minLength > 0 && (mb_strlen($value) < $constraint->minLength)) {
$this->context->buildViolation($constraint->tooShortMessage)
->setParameters(array('{{length}}' => $constraint->minLength))
->setParameters(['{{length}}' => $constraint->minLength])
->setInvalidValue($value)
->addViolation();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Constraints/PasswordStrength.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getDefaultOption()

public function getRequiredOptions()
{
return array('minStrength');
return ['minStrength'];
}

public function validatedBy()
Expand Down
8 changes: 4 additions & 4 deletions src/Validator/Constraints/PasswordStrengthValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function validate($password, Constraint $constraint)

if ($passLength < $constraint->minLength) {
$this->context->buildViolation($constraint->tooShortMessage)
->setParameters(array('{{length}}' => $constraint->minLength))
->setParameters(['{{length}}' => $constraint->minLength])
->addViolation();

return;
Expand All @@ -105,12 +105,12 @@ public function validate($password, Constraint $constraint)
// Detecting this is tricky and requires a deep understanding of the syntax.

if ($passwordStrength < $constraint->minStrength) {
$parameters = array(
$parameters = [
'{{ length }}' => $constraint->minLength,
'{{ min_strength }}' => $this->translator->trans('rollerworks_password.strength_level.'.self::$levelToLabel[$constraint->minStrength], [], 'validators'),
'{{ current_strength }}' => $this->translator->trans('rollerworks_password.strength_level.'.self::$levelToLabel[$passwordStrength], [], 'validators'),
'{{ strength_tips }}' => implode(', ', array_map(array($this, 'translateTips'), $tips)),
);
'{{ strength_tips }}' => implode(', ', array_map([$this, 'translateTips'], $tips)),
];

$this->context->buildViolation($constraint->message)
->setParameters($parameters)
Expand Down
2 changes: 1 addition & 1 deletion tests/Blacklist/ArrayProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ArrayProviderTest extends TestCase
{
public function testBlackList()
{
$provider = new ArrayProvider(array('test', 'foobar', 0));
$provider = new ArrayProvider(['test', 'foobar', 0]);

self::assertTrue($provider->isBlacklisted('test'));
self::assertTrue($provider->isBlacklisted('foobar'));
Expand Down
18 changes: 9 additions & 9 deletions tests/Blacklist/ChainProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class ChainProviderTest extends TestCase
public function testBlackList()
{
$provider = new ChainProvider();
$provider->addProvider(new ArrayProvider(array('test', 'foobar', 0)));
$provider->addProvider(new ArrayProvider(array('weak', 'god')));
$provider->addProvider(new ArrayProvider(['test', 'foobar', 0]));
$provider->addProvider(new ArrayProvider(['weak', 'god']));

self::assertTrue($provider->isBlacklisted('test'));
self::assertTrue($provider->isBlacklisted('foobar'));
Expand All @@ -37,25 +37,25 @@ public function testBlackList()

public function testProvidersByConstruct()
{
$provider1 = new ArrayProvider(array('test', 'foobar', 0));
$provider2 = new ArrayProvider(array('weak', 'god'));
$provider1 = new ArrayProvider(['test', 'foobar', 0]);
$provider2 = new ArrayProvider(['weak', 'god']);

$provider = new ChainProvider(array($provider1, $provider2));
$provider = new ChainProvider([$provider1, $provider2]);

self::assertEquals(array($provider1, $provider2), $provider->getProviders());
self::assertEquals([$provider1, $provider2], $provider->getProviders());
}

public function testGetProviders()
{
$provider = new ChainProvider();

$provider1 = new ArrayProvider(array('test', 'foobar', 0));
$provider2 = new ArrayProvider(array('weak', 'god'));
$provider1 = new ArrayProvider(['test', 'foobar', 0]);
$provider2 = new ArrayProvider(['weak', 'god']);

$provider->addProvider($provider1);
$provider->addProvider($provider2);

self::assertEquals(array($provider1, $provider2), $provider->getProviders());
self::assertEquals([$provider1, $provider2], $provider->getProviders());
}

public function testNoAssignSelf()
Expand Down
1 change: 0 additions & 1 deletion tests/Command/BlacklistCommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Rollerworks\Component\PasswordStrength\Blacklist\SqliteProvider;
use Symfony\Component\DependencyInjection\Container;

abstract class BlacklistCommandTestCase extends TestCase
{
Expand Down
20 changes: 10 additions & 10 deletions tests/Command/BlacklistDeleteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testDeleteOneWord()
self::assertTrue(self::$blackListProvider->isBlacklisted('foobar'));

$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName(), 'passwords' => 'test'));
$commandTester->execute(['command' => $command->getName(), 'passwords' => 'test']);

self::assertFalse(self::$blackListProvider->isBlacklisted('test'));
self::assertTrue(self::$blackListProvider->isBlacklisted('foobar'));
Expand All @@ -43,7 +43,7 @@ public function testDeleteNoneExistingWord()
self::assertFalse(self::$blackListProvider->isBlacklisted('test'));

$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName(), 'passwords' => 'test'));
$commandTester->execute(['command' => $command->getName(), 'passwords' => 'test']);

self::assertFalse(self::$blackListProvider->isBlacklisted('test'));
self::assertRegExp('/Successfully removed 0 password\(s\) from your blacklist database/', $commandTester->getDisplay());
Expand All @@ -58,7 +58,7 @@ public function testDeleteTwoWords()
self::$blackListProvider->add('kaboom');

$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName(), 'passwords' => array('test', 'foobar')));
$commandTester->execute(['command' => $command->getName(), 'passwords' => ['test', 'foobar']]);

self::assertFalse(self::$blackListProvider->isBlacklisted('test'));
self::assertFalse(self::$blackListProvider->isBlacklisted('foobar'));
Expand All @@ -72,7 +72,7 @@ public function testNoInput()
$command = $this->getCommand();

$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
$commandTester->execute(['command' => $command->getName()]);

self::assertNotRegExp('/Successfully removed \d+ password\(s\) from your blacklist database/', $commandTester->getDisplay());
self::assertRegExp('/No passwords or file-option given/', $commandTester->getDisplay());
Expand All @@ -88,7 +88,7 @@ public function testReadFromFile()

$commandTester = new CommandTester($command);

$commandTester->execute(array('command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list1.txt'));
$commandTester->execute(['command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list1.txt']);
self::assertRegExp('/Successfully removed 2 password\(s\) from your blacklist database/', $commandTester->getDisplay());

self::assertFalse(self::$blackListProvider->isBlacklisted('test'));
Expand All @@ -106,14 +106,14 @@ public function testImportExistingFromFile()

$commandTester = new CommandTester($command);

$commandTester->execute(array('command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list1.txt'));
$commandTester->execute(['command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list1.txt']);
self::assertRegExp('/Successfully removed 2 password\(s\) from your blacklist database/', $commandTester->getDisplay());

self::assertFalse(self::$blackListProvider->isBlacklisted('test'));
self::assertFalse(self::$blackListProvider->isBlacklisted('foobar'));
self::assertTrue(self::$blackListProvider->isBlacklisted('kaboom'));

$commandTester->execute(array('command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list1.txt'));
$commandTester->execute(['command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list1.txt']);
self::assertRegExp('/Successfully removed 0 password\(s\) from your blacklist database/', $commandTester->getDisplay());
}

Expand All @@ -130,7 +130,7 @@ public function testImportFromRelFile()
// This changes the current working directory to this one so we can check relative files
chdir(__DIR__);

$commandTester->execute(array('command' => $command->getName(), '--file' => '../fixtures/passwords-list1.txt'));
$commandTester->execute(['command' => $command->getName(), '--file' => '../fixtures/passwords-list1.txt']);
self::assertRegExp('/Successfully removed 2 password\(s\) from your blacklist database/', $commandTester->getDisplay());

self::assertFalse(self::$blackListProvider->isBlacklisted('test'));
Expand All @@ -147,7 +147,7 @@ public function testImportFromNoFile()

$commandTester = new CommandTester($command);
$commandTester->execute(
array('command' => $command->getName(), '--file' => '../fixtures/unknown.txt')
['command' => $command->getName(), '--file' => '../fixtures/unknown.txt']
);

self::assertRegExp('#Unable to read passwords list. No such file: \.\./fixtures/unknown\.txt#', $commandTester->getDisplay());
Expand All @@ -162,7 +162,7 @@ public function testImportFromEmptyFile()

$commandTester = new CommandTester($command);
$commandTester->execute(
array('command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list2.txt')
['command' => $command->getName(), '--file' => __DIR__.'/../fixtures/passwords-list2.txt']
);

self::assertRegExp('/Passwords list seems empty, are you sure this is the correct file\?/', $commandTester->getDisplay());
Expand Down
4 changes: 2 additions & 2 deletions tests/Command/BlacklistListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testList()

$command = $application->find('rollerworks-password:blacklist:list');

$blackListedWords = array('test', 'foobar', 'kaboom');
$blackListedWords = ['test', 'foobar', 'kaboom'];

foreach ($blackListedWords as $word) {
self::$blackListProvider->add($word);
Expand All @@ -38,7 +38,7 @@ public function testList()

$commandTester = new CommandTester($command);

$commandTester->execute(array('command' => $command->getName()));
$commandTester->execute(['command' => $command->getName()]);

$display = $commandTester->getDisplay(true);

Expand Down
Loading

0 comments on commit ea37af9

Please sign in to comment.