improved code styles #158 (#172)

* use php-cs-fixer instead of phpcodesniffer
* updated scrutinizer config
* adjusted code yules to all files
* updated CONTRIBUTING guidelines
This commit is contained in:
Kevin Papst
2018-06-22 21:09:10 +02:00
committed by GitHub
parent 773d27bb0e
commit cd7cbeae88
142 changed files with 775 additions and 467 deletions

View File

@@ -59,7 +59,7 @@ class CreateUserCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function configure()
{
@@ -74,7 +74,7 @@ class CreateUserCommand extends Command
->addArgument(
'role',
InputArgument::OPTIONAL,
'A comma separated list of user roles, e.g. "'.$roles.'"',
'A comma separated list of user roles, e.g. "' . $roles . '"',
User::DEFAULT_ROLE
)
->addArgument('password', InputArgument::OPTIONAL, 'Password for the new user (requested if not provided)')
@@ -82,7 +82,7 @@ class CreateUserCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
@@ -92,7 +92,7 @@ class CreateUserCommand extends Command
$email = $input->getArgument('email');
$role = $input->getArgument('role');
if ($input->getArgument('password') !== null) {
if (null !== $input->getArgument('password')) {
$password = $input->getArgument('password');
} else {
$password = $this->askForPassword($input, $output);
@@ -122,6 +122,7 @@ class CreateUserCommand extends Command
. $error->getMessage()
);
}
return;
}

View File

@@ -35,10 +35,9 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
*/
class KimaiImporterCommand extends Command
{
// minimum required Kimai and database version, lower versions are not supported by this command
const MIN_VERSION = '1.0.1';
const MIN_REVISION = '1388';
public const MIN_VERSION = '1.0.1';
public const MIN_REVISION = '1388';
/**
* Create the user default passwords
@@ -110,7 +109,7 @@ class KimaiImporterCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function configure()
{
@@ -130,7 +129,7 @@ class KimaiImporterCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
@@ -147,12 +146,14 @@ class KimaiImporterCommand extends Command
$password = $input->getArgument('password');
if (trim(strlen($password)) < 6) {
$io->error('Password length is not sufficient, at least 6 character are required');
return;
}
$country = $input->getArgument('country');
if (trim(strlen($country)) != 2) {
if (2 != trim(strlen($country))) {
$io->error('Country length needs to be exactly 2 character');
return;
}
@@ -174,6 +175,7 @@ class KimaiImporterCommand extends Command
$users = $this->fetchAllFromImport('users');
} catch (\Exception $ex) {
$io->error('Failed to load users: ' . $ex->getMessage());
return;
}
@@ -181,6 +183,7 @@ class KimaiImporterCommand extends Command
$customer = $this->fetchAllFromImport('customers');
} catch (\Exception $ex) {
$io->error('Failed to load customers: ' . $ex->getMessage());
return;
}
@@ -188,6 +191,7 @@ class KimaiImporterCommand extends Command
$projects = $this->fetchAllFromImport('projects');
} catch (\Exception $ex) {
$io->error('Failed to load projects: ' . $ex->getMessage());
return;
}
@@ -195,6 +199,7 @@ class KimaiImporterCommand extends Command
$activities = $this->fetchAllFromImport('activities');
} catch (\Exception $ex) {
$io->error('Failed to load activities: ' . $ex->getMessage());
return;
}
@@ -202,6 +207,7 @@ class KimaiImporterCommand extends Command
$activityToProject = $this->fetchAllFromImport('projects_activities');
} catch (\Exception $ex) {
$io->error('Failed to load activities-project mapping: ' . $ex->getMessage());
return;
}
@@ -209,6 +215,7 @@ class KimaiImporterCommand extends Command
$records = $this->fetchAllFromImport('timeSheet');
} catch (\Exception $ex) {
$io->error('Failed to load timeSheet: ' . $ex->getMessage());
return;
}
@@ -224,6 +231,7 @@ class KimaiImporterCommand extends Command
$io->success('Imported users: ' . $counter);
} catch (\Exception $ex) {
$io->error('Failed to import users: ' . $ex->getMessage());
return;
}
@@ -233,6 +241,7 @@ class KimaiImporterCommand extends Command
$io->success('Imported customers: ' . $counter);
} catch (\Exception $ex) {
$io->error('Failed to import customers: ' . $ex->getMessage());
return;
}
@@ -242,6 +251,7 @@ class KimaiImporterCommand extends Command
$io->success('Imported projects: ' . $counter);
} catch (\Exception $ex) {
$io->error('Failed to import projects: ' . $ex->getMessage());
return;
}
@@ -251,6 +261,7 @@ class KimaiImporterCommand extends Command
$io->success('Imported activities: ' . $counter);
} catch (\Exception $ex) {
$io->error('Failed to import activities: ' . $ex->getMessage());
return;
}
@@ -260,6 +271,7 @@ class KimaiImporterCommand extends Command
$io->success('Imported timesheet records: ' . $counter);
} catch (\Exception $ex) {
$io->error('Failed to import timesheet records: ' . $ex->getMessage());
return;
}
@@ -276,7 +288,7 @@ class KimaiImporterCommand extends Command
'Start: ' . $this->bytesHumanReadable($bytesStart) . PHP_EOL .
'After caching: ' . $this->bytesHumanReadable($bytesCached) . PHP_EOL .
'After import: ' . $this->bytesHumanReadable($bytesImported) . PHP_EOL .
'Total consumption for importing '.$allImports.' new database entries: ' .
'Total consumption for importing ' . $allImports . ' new database entries: ' .
$this->bytesHumanReadable($bytesImported - $bytesStart)
);
}
@@ -299,19 +311,21 @@ class KimaiImporterCommand extends Command
$version = $this->getImportConnection()->query($versionQuery)->fetchColumn();
$revision = $this->getImportConnection()->query($revisionQuery)->fetchColumn();
if (version_compare($requiredVersion, $version) == 1) {
if (1 == version_compare($requiredVersion, $version)) {
$io->error(
'Import can only performed from an up-to-date Kimai version:' . PHP_EOL .
'Needs at least ' . $requiredVersion . ' but found ' . $version
);
return false;
}
if (version_compare($requiredRevision, $revision) == 1) {
if (1 == version_compare($requiredRevision, $revision)) {
$io->error(
'Import can only performed from an up-to-date Kimai version:' . PHP_EOL .
'Database revision needs to be ' . $requiredRevision . ' but found ' . $revision
);
return false;
}
@@ -329,7 +343,7 @@ class KimaiImporterCommand extends Command
foreach ($allListener as $name => $listener) {
if (in_array($name, ['prePersist', 'preUpdate'])) {
foreach ($listener as $service => $class) {
if ($class === TimesheetSubscriber::class) {
if (TimesheetSubscriber::class === $class) {
$connection->getEventManager()->removeEventListener(['prePersist', 'preUpdate'], $class);
}
}
@@ -345,6 +359,7 @@ class KimaiImporterCommand extends Command
protected function bytesHumanReadable($size)
{
$unit = ['b', 'kB', 'MB', 'GB'];
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
@@ -389,11 +404,12 @@ class KimaiImporterCommand extends Command
$value = $error->getInvalidValue();
$io->error(
$error->getPropertyPath()
. " (" . (is_array($value) ? implode(',', $value) : $value) .")"
. ' (' . (is_array($value) ? implode(',', $value) : $value) . ')'
. "\n "
. $error->getMessage()
);
}
return false;
}
@@ -435,8 +451,8 @@ class KimaiImporterCommand extends Command
$entityManager = $this->getDoctrine()->getManager();
foreach ($users as $oldUser) {
$isActive = (bool)$oldUser['active'] && !(bool)$oldUser['trash'] && !(bool)$oldUser['ban'];
$role = ($oldUser['globalRoleID'] == 1) ? User::ROLE_SUPER_ADMIN : User::DEFAULT_ROLE;
$isActive = (bool) $oldUser['active'] && !(bool) $oldUser['trash'] && !(bool) $oldUser['ban'];
$role = (1 == $oldUser['globalRoleID']) ? User::ROLE_SUPER_ADMIN : User::DEFAULT_ROLE;
$user = new User();
$user->setUsername($oldUser['name'])
@@ -460,7 +476,7 @@ class KimaiImporterCommand extends Command
if ($this->debug) {
$io->success('Created user: ' . $user->getUsername());
}
$counter++;
++$counter;
} catch (\Exception $ex) {
$io->error('Failed to create user: ' . $user->getUsername());
$io->error('Reason: ' . $ex->getMessage());
@@ -468,6 +484,7 @@ class KimaiImporterCommand extends Command
$this->users[$oldUser['userID']] = $user;
}
return $counter;
}
@@ -508,7 +525,7 @@ class KimaiImporterCommand extends Command
$entityManager = $this->getDoctrine()->getManager();
foreach ($customers as $oldCustomer) {
$isActive = (bool)$oldCustomer['visible'] && !(bool)$oldCustomer['trash'];
$isActive = (bool) $oldCustomer['visible'] && !(bool) $oldCustomer['trash'];
$name = $oldCustomer['name'];
if (empty($name)) {
$name = uniqid();
@@ -540,7 +557,7 @@ class KimaiImporterCommand extends Command
if ($this->debug) {
$io->success('Created customer: ' . $customer->getName());
}
$counter++;
++$counter;
} catch (\Exception $ex) {
$io->error('Reason: ' . $ex->getMessage());
$io->error('Failed to create customer: ' . $customer->getName());
@@ -548,6 +565,7 @@ class KimaiImporterCommand extends Command
$this->customers[$oldCustomer['customerID']] = $customer;
}
return $counter;
}
@@ -577,7 +595,7 @@ class KimaiImporterCommand extends Command
$entityManager = $this->getDoctrine()->getManager();
foreach ($projects as $oldProject) {
$isActive = (bool)$oldProject['visible'] && !(bool)$oldProject['trash'];
$isActive = (bool) $oldProject['visible'] && !(bool) $oldProject['trash'];
$customer = $this->customers[$oldProject['customerID']];
$name = $oldProject['name'];
if (empty($name)) {
@@ -603,7 +621,7 @@ class KimaiImporterCommand extends Command
if ($this->debug) {
$io->success('Created project: ' . $project->getName() . ' for customer: ' . $customer->getName());
}
$counter++;
++$counter;
} catch (\Exception $ex) {
$io->error('Failed to create project: ' . $project->getName());
$io->error('Reason: ' . $ex->getMessage());
@@ -611,6 +629,7 @@ class KimaiImporterCommand extends Command
$this->projects[$oldProject['projectID']] = $project;
}
return $counter;
}
@@ -662,11 +681,12 @@ class KimaiImporterCommand extends Command
$this->unassignedActivities[$oldActivity['activityID']] = $oldActivity;
$this->createActivity($io, $entityManager, $project, $oldActivity);
$counter++;
++$counter;
} else {
$this->unassignedActivities[$oldActivity['activityID']] = $oldActivity;
}
}
return $counter;
}
@@ -689,7 +709,7 @@ class KimaiImporterCommand extends Command
return $this->activities[$activityId][$project->getId()];
}
$isActive = (bool)$oldActivity['visible'] && !(bool)$oldActivity['trash'];
$isActive = (bool) $oldActivity['visible'] && !(bool) $oldActivity['trash'];
$name = $oldActivity['name'];
if (empty($name)) {
$name = uniqid();
@@ -778,13 +798,13 @@ class KimaiImporterCommand extends Command
$activity = $this->activities[$activityId][$projectId];
}
if ($activity === null && isset($this->unassignedActivities[$activityId])) {
if (null === $activity && isset($this->unassignedActivities[$activityId])) {
$oldActivity = $this->unassignedActivities[$activityId];
$activity = $this->createActivity($io, $entityManager, $project, $oldActivity);
$activityCounter++;
++$activityCounter;
}
if ($activity === null) {
if (null === $activity) {
$io->error('Could not create timesheet record, missing activity with ID: ' . $activityId);
continue;
}
@@ -792,7 +812,7 @@ class KimaiImporterCommand extends Command
$duration = $oldRecord['end'] - $oldRecord['start'];
$rate = $oldRecord['fixedRate'];
if ((empty($rate) || $rate == 0.00) && !empty($oldRecord['rate'])) {
if ((empty($rate) || 0.00 == $rate) && !empty($oldRecord['rate'])) {
$hourlyRate = (float) $oldRecord['rate'];
$rate = (float) $hourlyRate * ($duration / 3600);
$rate = round($rate, 2);
@@ -802,8 +822,8 @@ class KimaiImporterCommand extends Command
$timesheet
->setDescription($oldRecord['description'] ?: ($oldRecord['comment'] ?: null))
->setUser($this->users[$oldRecord['userID']])
->setBegin(new \DateTime("@".$oldRecord['start']))
->setEnd(new \DateTime("@".$oldRecord['end']))
->setBegin(new \DateTime('@' . $oldRecord['start']))
->setEnd(new \DateTime('@' . $oldRecord['end']))
->setDuration($duration)
->setActivity($activity)
->setRate($rate)
@@ -819,13 +839,13 @@ class KimaiImporterCommand extends Command
if ($this->debug) {
$io->success('Created timesheet record: ' . $timesheet->getId());
}
$counter++;
++$counter;
} catch (\Exception $ex) {
$io->error('Failed to create timesheet record: ' . $timesheet->getId());
$io->error('Reason: ' . $ex->getMessage());
}
if ($counter % 500 == 0) {
if (0 == $counter % 500) {
$io->writeln('Imported ' . $counter . ' timesheet records, import ongoing ...');
}
}
@@ -833,6 +853,7 @@ class KimaiImporterCommand extends Command
if ($activityCounter > 0) {
$io->success('Created new (previously unattached) activities during timesheet import: ' . $activityCounter);
}
return $counter;
}
}

View File

@@ -22,9 +22,8 @@ use Symfony\Component\Console\Style\SymfonyStyle;
*/
class ResetCommand extends Command
{
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function configure()
{
@@ -56,6 +55,7 @@ EOT
$command->run(new ArrayInput([]), $output);
} catch (\Exception $ex) {
$io->error('Failed to create database: ' . $ex->getMessage());
return 1;
}
}
@@ -66,6 +66,7 @@ EOT
$command->run(new ArrayInput(['--force' => true]), $output);
} catch (\Exception $ex) {
$io->error('Failed to drop database schema: ' . $ex->getMessage());
return 2;
}
@@ -74,6 +75,7 @@ EOT
$command->run(new ArrayInput([]), $output);
} catch (\Exception $ex) {
$io->error('Failed to create database schema: ' . $ex->getMessage());
return 3;
}
}
@@ -85,6 +87,7 @@ EOT
$command->run($cmdInput, $output);
} catch (\Exception $ex) {
$io->error('Failed to import fixtures: ' . $ex->getMessage());
return 4;
}
@@ -94,6 +97,7 @@ EOT
$command->run(new ArrayInput([]), $output);
} catch (\Exception $ex) {
$io->error('Failed to clear cache: ' . $ex->getMessage());
return 5;
}
}
@@ -101,7 +105,6 @@ EOT
return 0;
}
/**
* @param InputInterface $input
* @param OutputInterface $output

View File

@@ -11,22 +11,22 @@ namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Command used to check the project coding styles.
* Command used to check and apply the projects coding styles.
*/
class RunCodeSnifferCommand extends Command
{
/**
* @var string
*/
protected $rootDir;
protected $rootDir = '';
/**
* RunCodeSnifferCommand constructor.
* @param $projectDirectory
* @param string $projectDirectory
*/
public function __construct($projectDirectory)
{
@@ -35,46 +35,64 @@ class RunCodeSnifferCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('kimai:phpcs')
->setDescription('Run PHP_CodeSniffer to check for the projects coding style')
->addOption('fix', null, InputOption::VALUE_NONE, 'Fix all found problems (risky: modifies your files)')
->addOption('checkstyle', null, InputOption::VALUE_OPTIONAL, '')
;
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$this->executeCodeSniffer($io, '/src');
$this->executeCodeSniffer($io, '/tests');
$this->executeCodeSniffer($io, '/templates');
}
$filename = null;
ob_start();
/**
* @param string $directory
*/
protected function executeCodeSniffer(SymfonyStyle $io, $directory)
{
$directory = $this->rootDir . $directory;
$args = [];
if (!$input->getOption('fix')) {
$filename = $input->getOption('checkstyle');
$args[] = '--dry-run';
$args[] = '--verbose';
$args[] = '--show-progress=none';
if (!empty($filename) && (file_exists($filename) && !is_writeable($filename))) {
$io->error('Target file is not writeable: ' . $filename);
return;
}
if (!empty($filename)) {
$filename = $this->rootDir . '/' . $filename;
$args[] = '> ' . $filename;
} else {
$args[] = '--format=txt';
}
}
$exitCode = 0;
ob_start();
passthru($this->rootDir . '/bin/phpcs --standard=PSR2 ' . $directory, $exitCode);
passthru($this->rootDir . '/vendor/bin/php-cs-fixer fix ' . implode(' ', $args), $exitCode);
$result = ob_get_clean();
$io->write($result);
if ($exitCode > 0) {
$io->error('Found problems while checking sources at: ' . $directory);
} else {
$io->success('All sources look good at: ' . $directory);
$io->error(
'Found problems while checking your code styles' .
(!empty($filename) ? '. Saved checkstyle data to: ' . $filename : '')
);
return;
}
$io->success('All source files have proper code styles');
}
}

View File

@@ -10,9 +10,6 @@
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Command used to run all integration tests.
@@ -20,7 +17,7 @@ use Symfony\Component\Console\Style\SymfonyStyle;
class RunIntegrationTestsCommand extends RunUnitTestsCommand
{
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function configure()
{

View File

@@ -19,7 +19,6 @@ use Symfony\Component\Console\Style\SymfonyStyle;
*/
class RunUnitTestsCommand extends Command
{
/**
* @var string
*/
@@ -36,7 +35,7 @@ class RunUnitTestsCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function configure()
{
@@ -48,7 +47,7 @@ class RunUnitTestsCommand extends Command
}
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{