Skip to content

Commit

Permalink
[PhpStan] checkAlwaysTrueCheckTypeFunctionCall: true (#14799)
Browse files Browse the repository at this point in the history
* [PhpStan] checkAlwaysTrueCheckTypeFunctionCall: true
* Followup to #15086
* Update phpstan version and update baseline
* Update composer.json
* Change count checks for arrays
* Throw exception if getPdf couldn't load the resource
  • Loading branch information
blankse committed Aug 1, 2023
1 parent cfa3ba7 commit 130fd27
Show file tree
Hide file tree
Showing 122 changed files with 974 additions and 1,132 deletions.
4 changes: 1 addition & 3 deletions bundles/CoreBundle/src/Command/Bundle/ListCommand.php
Expand Up @@ -96,9 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($input->getOption('json')) {
$jsonData = array_map(static function ($row) use ($returnData) {
return array_combine($returnData['headers'], $row);
}, $returnData['rows']);
$jsonData = array_map(fn($row) => array_combine($returnData['headers'], $row), $returnData['rows']);
$output->write(\json_encode($jsonData, \JSON_PRETTY_PRINT));
} else {
$table = new Table($output);
Expand Down
55 changes: 25 additions & 30 deletions bundles/CoreBundle/src/Command/DeleteUnusedLocaleDataCommand.php
Expand Up @@ -88,43 +88,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int

//drop unused localized view e.g. object_localized_classId_*
$existingViews = $db->fetchAllAssociative("SHOW TABLES LIKE 'object\_localized\_{$classId}\_%'");
foreach ($existingViews as $existingView) {
$localizedView = current($existingView);
$existingLanguage = str_replace('object_localized_'.$classId.'_', '', $localizedView);

if (is_array($existingViews)) {
foreach ($existingViews as $existingView) {
$localizedView = current($existingView);
$existingLanguage = str_replace('object_localized_'.$classId.'_', '', $localizedView);

if (!in_array($existingLanguage, $validLanguages)) {
$sqlDropView = 'DROP VIEW IF EXISTS object_localized_' . $classId . '_' .$existingLanguage;
$printLine = true;

if (!$this->isDryRun()) {
$output->writeln($sqlDropView);
$db->executeQuery($sqlDropView);
} else {
$output->writeln($this->dryRunMessage($sqlDropView));
}
if (!in_array($existingLanguage, $validLanguages)) {
$sqlDropView = 'DROP VIEW IF EXISTS object_localized_' . $classId . '_' .$existingLanguage;
$printLine = true;

if (!$this->isDryRun()) {
$output->writeln($sqlDropView);
$db->executeQuery($sqlDropView);
} else {
$output->writeln($this->dryRunMessage($sqlDropView));
}
}
}

//drop unused localized table e.g. object_localized_query_classId_*
$existingTables = $db->fetchAllAssociative("SHOW TABLES LIKE 'object\_localized\_query\_{$classId}\_%'");
if (is_array($existingTables)) {
foreach ($existingTables as $existingTable) {
$localizedTable = current($existingTable);
$existingLanguage = str_replace('object_localized_query_'.$classId.'_', '', $localizedTable);

if (!in_array($existingLanguage, $validLanguages)) {
$sqlDropTable = 'DROP TABLE IF EXISTS object_localized_query_' . $classId . '_' .$existingLanguage;
$printLine = true;

if (!$this->isDryRun()) {
$output->writeln($sqlDropTable);
$db->executeQuery($sqlDropTable);
} else {
$output->writeln($this->dryRunMessage($sqlDropTable));
}
foreach ($existingTables as $existingTable) {
$localizedTable = current($existingTable);
$existingLanguage = str_replace('object_localized_query_'.$classId.'_', '', $localizedTable);

if (!in_array($existingLanguage, $validLanguages)) {
$sqlDropTable = 'DROP TABLE IF EXISTS object_localized_query_' . $classId . '_' .$existingLanguage;
$printLine = true;

if (!$this->isDryRun()) {
$output->writeln($sqlDropTable);
$db->executeQuery($sqlDropTable);
} else {
$output->writeln($this->dryRunMessage($sqlDropTable));
}
}
}
Expand Down
Expand Up @@ -223,9 +223,6 @@ public function columnConfigAction(Request $request): JsonResponse
throw $this->createNotFoundException();
}
$columnConfiguration = $report->getColumnConfiguration();
if (!is_array($columnConfiguration)) {
$columnConfiguration = [];
}

$configuration = json_decode($request->get('configuration'));
$configuration = $configuration[0] ?? null;
Expand All @@ -238,9 +235,6 @@ public function columnConfigAction(Request $request): JsonResponse
try {
$adapter = Tool\Config::getAdapter($configuration);
$columns = $adapter->getColumns($configuration);
if (!is_array($columns)) {
$columns = [];
}

foreach ($columnConfiguration as $item) {
$name = $item['name'];
Expand Down
79 changes: 37 additions & 42 deletions bundles/CustomReportsBundle/src/Tool/Adapter/Sql.php
Expand Up @@ -132,55 +132,50 @@ protected function getBaseQuery(array $filters, array $fields, bool $ignoreSelec

$sql = $this->buildQueryString($this->config, $ignoreSelectAndGroupBy, $drillDownFilters, $selectField);

$data = '';
$extractAllFields = empty($fields);
if ($filters) {
if (is_array($filters)) {
foreach ($filters as $filter) {
$value = $filter['value'] ?? null;
$type = $filter['type'];
$operator = $filter['operator'];
$maxValue = null;
foreach ($filters as $filter) {
$value = $filter['value'] ?? null;
$type = $filter['type'];
$operator = $filter['operator'];
$maxValue = null;
if ($type == 'date') {
if ($operator == 'eq') {
$maxValue = strtotime($value . '+23 hours 59 minutes');
}
$value = strtotime($value);
}

switch ($operator) {
case 'like':
$fields[] = $filter['property'];
$condition[] = $db->quoteIdentifier($filter['property']) . ' LIKE ' . $db->quote('%' . $value. '%');

break;
case 'lt':
case 'gt':
case 'eq':
$compMapping = [
'lt' => '<',
'gt' => '>',
'eq' => '=',
];

if ($type == 'date') {
if ($operator == 'eq') {
$maxValue = strtotime($value . '+23 hours 59 minutes');
}
$value = strtotime($value);
}

switch ($operator) {
case 'like':
$fields[] = $filter['property'];
$condition[] = $db->quoteIdentifier($filter['property']) . ' LIKE ' . $db->quote('%' . $value. '%');
$condition[] = $db->quoteIdentifier($filter['property']) . ' BETWEEN ' . $db->quote($value) . ' AND ' . $db->quote($maxValue);

break;
case 'lt':
case 'gt':
case 'eq':
$compMapping = [
'lt' => '<',
'gt' => '>',
'eq' => '=',
];

if ($type == 'date') {
if ($operator == 'eq') {
$condition[] = $db->quoteIdentifier($filter['property']) . ' BETWEEN ' . $db->quote($value) . ' AND ' . $db->quote($maxValue);

break;
}
}
$fields[] = $filter['property'];
$condition[] = $db->quoteIdentifier($filter['property']) . ' ' . $compMapping[$operator] . ' ' . $db->quote($value);
}
}
$fields[] = $filter['property'];
$condition[] = $db->quoteIdentifier($filter['property']) . ' ' . $compMapping[$operator] . ' ' . $db->quote($value);

break;
case '=':
$fields[] = $filter['property'];
$condition[] = $db->quoteIdentifier($filter['property']) . ' = ' . $db->quote($value);
break;
case '=':
$fields[] = $filter['property'];
$condition[] = $db->quoteIdentifier($filter['property']) . ' = ' . $db->quote($value);

break;
}
}
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion bundles/CustomReportsBundle/src/Tool/Config.php
Expand Up @@ -228,7 +228,7 @@ public function setDataSourceConfig(array $dataSourceConfig): void

public function getDataSourceConfig(): ?\stdClass
{
if (is_array($this->dataSourceConfig) && isset($this->dataSourceConfig[0])) {
if (isset($this->dataSourceConfig[0])) {
$dataSourceConfig = new \stdClass();
$dataSourceConfigArray = $this->dataSourceConfig[0];

Expand Down
Expand Up @@ -42,7 +42,7 @@ public function getFilters(): array

public function applyGlossary(string $string, array $options = []): string
{
if (empty($string) || !is_string($string)) {
if (!$string) {
return $string;
}

Expand Down
Expand Up @@ -43,7 +43,7 @@ public function onURLSlugUpdate(UrlSlugEvent $event): void
$pimcore_seo_redirects = Pimcore::getContainer()->getParameter('pimcore_seo.redirects');
$data = $event->getData();
// check for previous slugs and create redirects
if (!is_array($data) || !$pimcore_seo_redirects['auto_create_redirects']) {
if (!$pimcore_seo_redirects['auto_create_redirects']) {
return;
}

Expand Down
3 changes: 3 additions & 0 deletions bundles/SeoBundle/src/Redirect/Csv.php
Expand Up @@ -34,6 +34,9 @@
*/
class Csv
{
/**
* @var string[]
*/
private array $columns = [
'id',
'type',
Expand Down
Expand Up @@ -187,7 +187,7 @@ public function findAction(Request $request, EventDispatcherInterface $eventDisp
}
}

if (is_array($types) && !empty($types[0])) {
if ($types[0]) {
$conditionTypeParts = [];
foreach ($types as $type) {
$conditionTypeParts[] = $db->quote($type);
Expand All @@ -198,15 +198,15 @@ public function findAction(Request $request, EventDispatcherInterface $eventDisp
$conditionParts[] = '( maintype IN (' . implode(',', $conditionTypeParts) . ') )';
}

if (is_array($subtypes) && !empty($subtypes[0])) {
if ($subtypes[0]) {
$conditionSubtypeParts = [];
foreach ($subtypes as $subtype) {
$conditionSubtypeParts[] = $db->quote($subtype);
}
$conditionParts[] = '( `type` IN (' . implode(',', $conditionSubtypeParts) . ') )';
}

if (is_array($classnames) && !empty($classnames[0])) {
if ($classnames[0]) {
if (in_array('folder', $subtypes)) {
$classnames[] = 'folder';
}
Expand All @@ -222,10 +222,8 @@ public function findAction(Request $request, EventDispatcherInterface $eventDisp
$tagIds = $allParams['tagIds'];

$tagsTypeCondition = '';
if (is_array($types) && !empty($types[0])) {
if ($types[0]) {
$tagsTypeCondition = 'ctype IN (\'' . implode('\',\'', $types) . '\') AND';
} elseif (!is_array($types)) {
$tagsTypeCondition = 'ctype = ' . $db->quote($types) . ' AND ';
}

foreach ($tagIds as $tagId) {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public function searchData(int $id, string $firstname, string $lastname, string
}
}

if (is_array($classnames) && !empty($classnames[0])) {
if ($classnames) {
$conditionClassnameParts = [];
foreach ($classnames as $classname) {
$conditionClassnameParts[] = $db->quote($classname);
Expand Down
34 changes: 15 additions & 19 deletions bundles/SimpleBackendSearchBundle/src/Model/Search/Backend/Data.php
Expand Up @@ -337,15 +337,13 @@ public function setDataFromElement(Element\ElementInterface $element): static

$this->properties = '';
$properties = $element->getProperties();
if (is_array($properties)) {
foreach ($properties as $nextProperty) {
$pData = (string) $nextProperty->getData();
if ($nextProperty->getName() === 'bool') {
$pData = $pData ? 'true' : 'false';
}

$this->properties .= $nextProperty->getName() . ':' . $pData .' ';
foreach ($properties as $nextProperty) {
$pData = (string) $nextProperty->getData();
if ($nextProperty->getName() === 'bool') {
$pData = $pData ? 'true' : 'false';
}

$this->properties .= $nextProperty->getName() . ':' . $pData .' ';
}

$this->data = '';
Expand All @@ -359,18 +357,16 @@ public function setDataFromElement(Element\ElementInterface $element): static
} elseif ($element instanceof Document\PageSnippet) {
$this->published = $element->isPublished();
$editables = $element->getEditables();
if (is_array($editables) && !empty($editables)) {
foreach ($editables as $editable) {
if ($editable instanceof Document\Editable\EditableInterface) {
// areabrick elements are handled by getElementTypes()/getElements() as they return area elements as well
if ($editable instanceof Document\Editable\Area || $editable instanceof Document\Editable\Areablock) {
continue;
}

ob_start();
$this->data .= strip_tags((string) $editable->frontend()).' ';
$this->data .= ob_get_clean();
foreach ($editables as $editable) {
if ($editable instanceof Document\Editable\EditableInterface) {
// areabrick elements are handled by getElementTypes()/getElements() as they return area elements as well
if ($editable instanceof Document\Editable\Area || $editable instanceof Document\Editable\Areablock) {
continue;
}

ob_start();
$this->data .= strip_tags((string) $editable->frontend()).' ';
$this->data .= ob_get_clean();
}
}
if ($element instanceof Document\Page) {
Expand Down
Expand Up @@ -111,9 +111,7 @@ public function staticroutesAction(Request $request): JsonResponse
foreach ($list->getRoutes() as $routeFromList) {
$route = $routeFromList->getObjectVars();
$route['writeable'] = $routeFromList->isWriteable();
if (is_array($routeFromList->getSiteId())) {
$route['siteId'] = implode(',', $routeFromList->getSiteId());
}
$route['siteId'] = implode(',', $routeFromList->getSiteId());
$routes[] = $route;
}

Expand Down
12 changes: 5 additions & 7 deletions bundles/StaticRoutesBundle/src/Model/Staticroute.php
Expand Up @@ -481,13 +481,11 @@ public function match(string $path, array $params = []): bool|array

preg_match_all($this->getPattern(), $path, $matches);

if (is_array($matches) && count($matches) > 1) {
foreach ($matches as $index => $match) {
if (isset($variables[$index - 1]) && $variables[$index - 1]) {
$paramValue = urldecode($match[0]);
if (!empty($paramValue) || !array_key_exists($variables[$index - 1], $params)) {
$params[$variables[$index - 1]] = $paramValue;
}
foreach ($matches as $index => $match) {
if (isset($variables[$index - 1]) && $variables[$index - 1]) {
$paramValue = urldecode($match[0]);
if (!empty($paramValue) || !array_key_exists($variables[$index - 1], $params)) {
$params[$variables[$index - 1]] = $paramValue;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -140,8 +140,8 @@
"codeception/module-symfony": "^3.1.0",
"codeception/phpunit-wrapper": "^9",
"ergebnis/phpstan-rules": "^2.0",
"phpstan/phpstan": "1.10.5",
"phpstan/phpstan-symfony": "^1.2.20",
"phpstan/phpstan": "1.10.26",
"phpstan/phpstan-symfony": "^1.3.2",
"phpunit/phpunit": "^9.3",
"gotenberg/gotenberg-php": "^1.1",
"composer/composer": "*",
Expand Down
6 changes: 1 addition & 5 deletions lib/Cache/FullPage/SessionStatus.php
Expand Up @@ -50,11 +50,7 @@ public function isDisabledBySession(Request $request): bool

// we fall back to $_SESSION from here on as the session API does not expose a list of namespaces
$sessionData = $_SESSION ?? null;
if (empty($sessionData)) {
return false;
}

if (!is_array($sessionData)) {
if (!$sessionData) {
return false;
}

Expand Down
7 changes: 1 addition & 6 deletions lib/Composer.php
Expand Up @@ -159,13 +159,8 @@ protected static function getPhp(bool $includeArgs = true): string
*/
protected static function getPhpArguments(): array
{
$ini = null;
$arguments = [];

$phpFinder = new PhpExecutableFinder();
if (method_exists($phpFinder, 'findArguments')) {
$arguments = $phpFinder->findArguments();
}
$arguments = $phpFinder->findArguments();

if(!empty($_SERVER['COMPOSER_ORIGINAL_INIS'])) {
$paths = explode(PATH_SEPARATOR, $_SERVER['COMPOSER_ORIGINAL_INIS']);
Expand Down

0 comments on commit 130fd27

Please sign in to comment.