Code improvements (#1649)
* use global namespace for faster lookups * phpstan level 5
This commit is contained in:
@@ -102,7 +102,7 @@ final class CreateUserCommand extends Command
|
||||
$value = $error->getInvalidValue();
|
||||
$io->error(
|
||||
$error->getPropertyPath()
|
||||
. ' (' . (is_array($value) ? implode(',', $value) : $value) . ')'
|
||||
. ' (' . (\is_array($value) ? implode(',', $value) : $value) . ')'
|
||||
. "\n "
|
||||
. $error->getMessage()
|
||||
);
|
||||
|
||||
@@ -266,12 +266,12 @@ class ImportCustomerCommand extends Command
|
||||
|
||||
private function getCustomer(string $customerName): Customer
|
||||
{
|
||||
if (!array_key_exists($customerName, $this->customerCache)) {
|
||||
if (!\array_key_exists($customerName, $this->customerCache)) {
|
||||
$tmpCustomer = $this->customers->findBy(['name' => $customerName]);
|
||||
|
||||
if (count($tmpCustomer) > 1) {
|
||||
if (\count($tmpCustomer) > 1) {
|
||||
throw new \Exception(sprintf('Found multiple customers with the name: %s', $customerName));
|
||||
} elseif (count($tmpCustomer) === 1) {
|
||||
} elseif (\count($tmpCustomer) === 1) {
|
||||
$tmpCustomer = $tmpCustomer[0];
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ class ImportCustomerCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists($customerName, $this->customerCache)) {
|
||||
if (\array_key_exists($customerName, $this->customerCache)) {
|
||||
return $this->customerCache[$customerName];
|
||||
}
|
||||
|
||||
@@ -328,7 +328,7 @@ class ImportCustomerCommand extends Command
|
||||
$fields = [];
|
||||
|
||||
foreach (self::$requiredHeader as $headerName) {
|
||||
if (!in_array($headerName, $header)) {
|
||||
if (!\in_array($headerName, $header)) {
|
||||
$fields[] = $headerName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ class ImportTimesheetCommand extends Command
|
||||
|
||||
$activityType = $input->getOption('activity');
|
||||
$allowedActivityTypes = ['project', 'global'];
|
||||
if (!in_array($activityType, $allowedActivityTypes)) {
|
||||
if (!\in_array($activityType, $allowedActivityTypes)) {
|
||||
$io->error(sprintf('Invalid activity type "%s" given, allowed values are: %s', $activityType, implode(', ', $allowedActivityTypes)));
|
||||
|
||||
return 4;
|
||||
@@ -274,7 +274,7 @@ class ImportTimesheetCommand extends Command
|
||||
$duration = 0;
|
||||
|
||||
if (!empty($record['Duration'])) {
|
||||
if (is_int($record['Duration'])) {
|
||||
if (\is_int($record['Duration'])) {
|
||||
$duration = $record['Duration'];
|
||||
} else {
|
||||
$duration = $durationParser->parseDurationString($record['Duration']);
|
||||
@@ -342,7 +342,7 @@ class ImportTimesheetCommand extends Command
|
||||
|
||||
private function getUser($user): User
|
||||
{
|
||||
if (!array_key_exists($user, $this->userCache)) {
|
||||
if (!\array_key_exists($user, $this->userCache)) {
|
||||
$tmpUser = $this->users->findOneBy(['username' => $user]);
|
||||
if (null === $tmpUser) {
|
||||
$tmpUser = $this->users->findOneBy(['email' => $user]);
|
||||
@@ -362,9 +362,9 @@ class ImportTimesheetCommand extends Command
|
||||
|
||||
$tmpActivities = $this->activities->findBy(['project' => $project, 'name' => $activity]);
|
||||
|
||||
if (count($tmpActivities) === 0) {
|
||||
if (\count($tmpActivities) === 0) {
|
||||
$tmpActivity = $this->activities->findOneBy(['project' => null, 'name' => $activity]);
|
||||
} elseif (count($tmpActivities) === 1) {
|
||||
} elseif (\count($tmpActivities) === 1) {
|
||||
$tmpActivity = $tmpActivities[0];
|
||||
}
|
||||
|
||||
@@ -390,7 +390,7 @@ class ImportTimesheetCommand extends Command
|
||||
/** @var Project[] $tmpProjects */
|
||||
$tmpProjects = $this->projects->findBy(['name' => $project]);
|
||||
|
||||
if (count($tmpProjects) > 1) {
|
||||
if (\count($tmpProjects) > 1) {
|
||||
/** @var Project $prj */
|
||||
foreach ($tmpProjects as $prj) {
|
||||
if ($prj->getCustomer()->getName() !== $tmpCustomer->getName()) {
|
||||
@@ -399,7 +399,7 @@ class ImportTimesheetCommand extends Command
|
||||
$tmpProject = $prj;
|
||||
break;
|
||||
}
|
||||
} elseif (count($tmpProjects) === 1) {
|
||||
} elseif (\count($tmpProjects) === 1) {
|
||||
$tmpProject = $tmpProjects[0];
|
||||
}
|
||||
|
||||
@@ -423,11 +423,11 @@ class ImportTimesheetCommand extends Command
|
||||
private function getCustomer($customer, $fallback): Customer
|
||||
{
|
||||
if (!empty($customer)) {
|
||||
if (!array_key_exists($customer, $this->customerCache)) {
|
||||
if (!\array_key_exists($customer, $this->customerCache)) {
|
||||
$tmpCustomer = $this->customers->findBy(['name' => $customer]);
|
||||
if (count($tmpCustomer) > 1) {
|
||||
if (\count($tmpCustomer) > 1) {
|
||||
throw new \Exception(sprintf('Found multiple customers with the name: %s', $customer));
|
||||
} elseif (count($tmpCustomer) === 1) {
|
||||
} elseif (\count($tmpCustomer) === 1) {
|
||||
$tmpCustomer = $tmpCustomer[0];
|
||||
}
|
||||
|
||||
@@ -436,7 +436,7 @@ class ImportTimesheetCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists($customer, $this->customerCache)) {
|
||||
if (\array_key_exists($customer, $this->customerCache)) {
|
||||
return $this->customerCache[$customer];
|
||||
}
|
||||
}
|
||||
@@ -445,7 +445,7 @@ class ImportTimesheetCommand extends Command
|
||||
$tmpFallback = null;
|
||||
|
||||
if (!empty($fallback)) {
|
||||
if (is_int($customer)) {
|
||||
if (\is_int($customer)) {
|
||||
$tmpFallback = $this->customers->find($fallback);
|
||||
} else {
|
||||
/** @var Customer|null $tmpFallback */
|
||||
@@ -455,7 +455,7 @@ class ImportTimesheetCommand extends Command
|
||||
|
||||
if (null === $tmpFallback) {
|
||||
$newName = self::DEFAULT_CUSTOMER;
|
||||
if (!empty($fallback) && is_string($fallback)) {
|
||||
if (!empty($fallback) && \is_string($fallback)) {
|
||||
$newName = $fallback;
|
||||
}
|
||||
$tmpFallback = new Customer();
|
||||
|
||||
@@ -346,7 +346,7 @@ class InvoiceCreateCommand extends Command
|
||||
$columns = ['ID', 'Customer', 'Total', 'Filename'];
|
||||
|
||||
$table = new Table($output);
|
||||
$table->setHeaderTitle(sprintf('Created %s invoice(s)', count($invoices)));
|
||||
$table->setHeaderTitle(sprintf('Created %s invoice(s)', \count($invoices)));
|
||||
$table->setHeaders($columns);
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
|
||||
@@ -166,21 +166,21 @@ final class KimaiImporterCommand extends Command
|
||||
$this->dbPrefix = $input->getArgument('prefix');
|
||||
|
||||
$password = $input->getArgument('password');
|
||||
if (trim(strlen($password)) < 6) {
|
||||
$io->error('Password length is not sufficient, at least 6 character are required');
|
||||
if (null === $password || \strlen($password = trim($password)) < 8) {
|
||||
$io->error('Password length is not sufficient, at least 8 character are required');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$country = $input->getArgument('country');
|
||||
if (2 != trim(strlen($country))) {
|
||||
if (null === $country || 2 != \strlen($country = trim($country))) {
|
||||
$io->error('Country code needs to be exactly 2 character');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$currency = $input->getArgument('currency');
|
||||
if (3 != trim(strlen($currency))) {
|
||||
if (null === $currency || 3 != \strlen($currency = trim($currency))) {
|
||||
$io->error('Currency code needs to be exactly 3 character');
|
||||
|
||||
return 1;
|
||||
@@ -300,7 +300,7 @@ final class KimaiImporterCommand extends Command
|
||||
$validationMessages[] = sprintf('User "%s" with ID %s has no email', $oldUser['name'], $oldUser['userID']);
|
||||
continue;
|
||||
}
|
||||
if (in_array($oldUser['mail'], $usedEmails)) {
|
||||
if (\in_array($oldUser['mail'], $usedEmails)) {
|
||||
$validationMessages[] = sprintf('Email "%s" for user "%s" with ID %s is already used', $oldUser['mail'], $oldUser['name'], $oldUser['userID']);
|
||||
}
|
||||
$usedEmails[] = $oldUser['mail'];
|
||||
@@ -312,7 +312,7 @@ final class KimaiImporterCommand extends Command
|
||||
}
|
||||
|
||||
foreach ($projects as $oldProject) {
|
||||
if (!in_array($oldProject['customerID'], $customerIds)) {
|
||||
if (!\in_array($oldProject['customerID'], $customerIds)) {
|
||||
$validationMessages[] = sprintf('Project "%s" with ID %s has unknown customer with ID %s', $oldProject['name'], $oldProject['projectID'], $oldProject['customerID']);
|
||||
}
|
||||
}
|
||||
@@ -465,12 +465,10 @@ final class KimaiImporterCommand extends Command
|
||||
protected function deactivateLifecycleCallbacks(Connection $connection)
|
||||
{
|
||||
$allListener = $connection->getEventManager()->getListeners();
|
||||
foreach ($allListener as $name => $listener) {
|
||||
if (in_array($name, ['prePersist', 'preUpdate'])) {
|
||||
foreach ($listener as $service => $class) {
|
||||
if (TimesheetSubscriber::class === $class) {
|
||||
$connection->getEventManager()->removeEventListener(['prePersist', 'preUpdate'], $class);
|
||||
}
|
||||
foreach ($allListener as $event => $listeners) {
|
||||
foreach ($listeners as $hash => $object) {
|
||||
if ($object instanceof TimesheetSubscriber) {
|
||||
$connection->getEventManager()->removeEventListener([$event], $object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -602,7 +600,7 @@ final class KimaiImporterCommand extends Command
|
||||
foreach ($preferences as $pref) {
|
||||
$key = $pref['option'];
|
||||
|
||||
if (!array_key_exists($key, $prefsToImport)) {
|
||||
if (!\array_key_exists($key, $prefsToImport)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -951,7 +949,7 @@ final class KimaiImporterCommand extends Command
|
||||
* @param array $oldActivity
|
||||
* @param array $fixedRates
|
||||
* @param array $rates
|
||||
* @param int $oldProjectId
|
||||
* @param int|null $oldProjectId
|
||||
* @return Activity
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -961,7 +959,7 @@ final class KimaiImporterCommand extends Command
|
||||
array $oldActivity,
|
||||
array $fixedRates,
|
||||
array $rates,
|
||||
$oldProjectId
|
||||
$oldProjectId = null
|
||||
) {
|
||||
$oldActivityId = $oldActivity['activityID'];
|
||||
|
||||
@@ -1119,7 +1117,7 @@ final class KimaiImporterCommand extends Command
|
||||
$activityCounter = 0;
|
||||
$userCounter = 0;
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$total = count($records);
|
||||
$total = \count($records);
|
||||
|
||||
$io->writeln('Importing timesheets, please wait');
|
||||
|
||||
@@ -1256,7 +1254,7 @@ final class KimaiImporterCommand extends Command
|
||||
->setDuration($duration)
|
||||
->setActivity($activity)
|
||||
->setProject($project)
|
||||
->setExported(intval($oldRecord['cleared']) !== 0)
|
||||
->setExported(\intval($oldRecord['cleared']) !== 0)
|
||||
->setTimezone($timezone)
|
||||
;
|
||||
|
||||
@@ -1299,7 +1297,7 @@ final class KimaiImporterCommand extends Command
|
||||
if ($activityCounter > 0) {
|
||||
$io->success('Created new activities during timesheet import: ' . $activityCounter);
|
||||
}
|
||||
if (count($errors['projectActivityMismatch']) > 0) {
|
||||
if (\count($errors['projectActivityMismatch']) > 0) {
|
||||
$io->error('Found invalid mapped project - activity combinations in these old timesheet recors: ' . implode(',', $errors['projectActivityMismatch']));
|
||||
}
|
||||
if ($failed > 0) {
|
||||
@@ -1446,9 +1444,9 @@ final class KimaiImporterCommand extends Command
|
||||
sprintf(
|
||||
'Created team: %s with %s users, %s projects and %s customers.',
|
||||
$team->getName(),
|
||||
count($team->getUsers()),
|
||||
count($team->getProjects()),
|
||||
count($team->getCustomers())
|
||||
\count($team->getUsers()),
|
||||
\count($team->getProjects()),
|
||||
\count($team->getCustomers())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user