Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a lot of psalm issues #2628

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
}
],
"require": {
"ext-libxml": "*"
"ext-libxml": "*",
"ext-dom": "*"
},
"require-dev": {
"nextcloud/coding-standard": "^1.0.0",
"christophwurst/nextcloud_testing": "^1.0.0",
"psalm/phar": "^5.12",
"nextcloud/ocp": "^27.0"
"nextcloud/ocp": "dev-stable28"
},
"scripts": {
"cs:check": "./vendor/bin/php-cs-fixer fix --dry-run --diff",
Expand All @@ -31,7 +32,7 @@
},
"config": {
"platform": {
"php": "7.4"
"php": "8.0"
}
}
}
29 changes: 16 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Db/RecipeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @deprecated
* @todo Why deprecated?

Check warning on line 36 in lib/Db/RecipeDb.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Why deprecated?
*/
public function findRecipeById(int $id) {
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -349,7 +349,7 @@
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
*/
public function findRecipes(array $keywords, string $user_id) {
$has_keywords = $keywords && is_array($keywords) && $keywords[0];
$has_keywords = $keywords && $keywords[0];

if (!$has_keywords) {
return $this->findAllRecipes($user_id);
Expand Down
4 changes: 2 additions & 2 deletions lib/Helper/Filter/JSON/FixInstructionsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* After that, some cleanup is done to prevent malicious code insertions.
*
* @todo Parse HTML entries for bold/italic/underlined text and create markdown

Check warning on line 26 in lib/Helper/Filter/JSON/FixInstructionsFilter.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Parse HTML entries for bold/italic/underlined text and create markdown
*/
class FixInstructionsFilter extends AbstractJSONFilter {
private const INSTRUCTIONS = 'recipeInstructions';
Expand Down Expand Up @@ -107,7 +107,7 @@
}

ksort($instructions);
$instructions = array_merge(...$instructions);
$instructions = array_merge([], ...$instructions);
}

$instructions = array_map(function ($x) {
Expand Down Expand Up @@ -160,7 +160,7 @@
}

ksort($newElements);
$elements = array_merge(...$newElements);
$elements = array_merge([], ...$newElements);

return $elements;
}
Expand Down
12 changes: 7 additions & 5 deletions lib/Helper/Filter/JSON/SchemaConformityFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
class SchemaConformityFilter extends AbstractJSONFilter {
public function apply(array &$json): bool {
$changed = false;

$changed |= $this->setJSONValue($json, '@context', 'http://schema.org');
$changed |= $this->setJSONValue($json, '@type', 'Recipe');

return (bool)$changed;
if ($this->setJSONValue($json, '@context', 'http://schema.org')) {
$changed = true;
}
if ($this->setJSONValue($json, '@type', 'Recipe')) {
$changed = true;
}
return $changed;
}
}
13 changes: 7 additions & 6 deletions lib/Helper/HTMLParser/HttpMicrodataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\Cookbook\Helper\HTMLParser;

use DOMDocument;
use DOMElement;
use DOMNode;
use DOMNodeList;
use DOMXPath;
Expand All @@ -12,9 +13,9 @@
/**
* This class is an AbstractHtmlParser which tries to extract micro data from the HTML page.
* @author Christian Wolf
* @todo Nutrition data is missing

Check warning on line 16 in lib/Helper/HTMLParser/HttpMicrodataParser.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Nutrition data is missing
* @todo Category needs checking

Check warning on line 17 in lib/Helper/HTMLParser/HttpMicrodataParser.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Category needs checking
* @todo Tools need to be imported

Check warning on line 18 in lib/Helper/HTMLParser/HttpMicrodataParser.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Tools need to be imported
*/
class HttpMicrodataParser extends AbstractHtmlParser {
/**
Expand Down Expand Up @@ -58,9 +59,9 @@
/**
* Parse a DOM node that represents a recipe
*
* @param DOMNode $recipeNode The DOM node to parse
* @param DOMElement $recipeNode The DOM node to parse
*/
private function parseRecipe(DOMNode $recipeNode): void {
private function parseRecipe(DOMElement $recipeNode): void {
$this->searchSimpleProperties($recipeNode, 'name');
$this->searchSimpleProperties($recipeNode, 'keywords');
$this->searchSimpleProperties($recipeNode, 'category');
Expand All @@ -75,9 +76,9 @@

/**
* Make one final desperate attempt at getting the instructions
* @param DOMNode $recipeNode The recipe node to use
* @param DOMElement $recipeNode The recipe node to use
*/
private function fixupInstructions(DOMNode $recipeNode): void {
private function fixupInstructions(DOMElement $recipeNode): void {
if (
!isset($this->recipe['recipeInstructions']) ||
!$this->recipe['recipeInstructions'] || sizeof($this->recipe['recipeInstructions']) < 1
Expand Down Expand Up @@ -254,12 +255,12 @@
* This can be used to extract a single attribute from the DOM tree.
* The attributes are evaluated first to last and the first found attribute is returned.
*
* @param DOMNode $node The node to evaluate
* @param DOMElement $node The node to evaluate
* @param array $attributes The possible attributes to check
* @throws AttributeNotFoundException If none of the named attributes is found
* @return string The value of the attribute
*/
private function extractSingeAttribute(DOMNode $node, array $attributes): string {
private function extractSingeAttribute(DOMElement $node, array $attributes): string {
foreach ($attributes as $attr) {
if ($node->hasAttribute($attr) && !empty($node->getAttribute($attr))) {
return $node->getAttribute($attr);
Expand Down
4 changes: 4 additions & 0 deletions lib/Helper/ImageService/ThumbnailFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace OCA\Cookbook\Helper\ImageService;

use OCA\Cookbook\Exception\InvalidThumbnailTypeException;
use OCA\Cookbook\Exception\NoRecipeImageFoundException;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\Lock\LockedException;

/**
* This class allows to handle the files of the thumbnails
Expand Down
2 changes: 2 additions & 0 deletions lib/Helper/RestParameterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function getParameters(): array {
}

break;
default:
throw new \Exception($this->l->t('Unsupported type of transmitted data. This is a bug, please report it.'));
}
} else {
throw new \Exception($this->l->t('Cannot detect type of transmitted data. This is a bug, please report it.'));
Expand Down
2 changes: 1 addition & 1 deletion lib/Helper/UserConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function getUpdateInterval(): int {
* @throws UserNotLoggedInException if no user is logged in
*/
public function setUpdateInterval(int $value): void {
$this->setRawValue(self::KEY_UPDATE_INTERVAL, $value);
$this->setRawValue(self::KEY_UPDATE_INTERVAL, (string)$value);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace OCA\Cookbook\Service;

use OCA\Cookbook\Exception\InvalidThumbnailTypeException;
use OCA\Cookbook\Exception\NoRecipeImageFoundException;
use OCA\Cookbook\Exception\RecipeImageExistsException;
use OCA\Cookbook\Helper\ImageService\ImageFileHelper;
use OCA\Cookbook\Helper\ImageService\ThumbnailFileHelper;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\GenericFileException;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Lock\LockedException;
Expand All @@ -14,8 +19,8 @@
* This class provides an interface to access the images of the recipes.
* This simplifies/abstracts the access of the images to avoid low-level file system access.
*
* @todo Use Abstract Filesystem

Check warning on line 22 in lib/Service/ImageService.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Use Abstract Filesystem
* @todo Rework the exeption passing

Check warning on line 23 in lib/Service/ImageService.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found @todo Rework the exeption passing
*/
class ImageService {
/**
Expand Down
13 changes: 5 additions & 8 deletions lib/Service/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@
if (isset($json['id']) && $json['id']) {
// Recipe already has an id, update it
$recipe_folder = $user_folder->getById($json['id'])[0];
if (!($recipe_folder instanceof Folder)) {
throw new \RuntimeException($this->il10n->t('Unexpected node received for recipe folder.'));
}

$old_path = $recipe_folder->getPath();
$new_path = dirname($old_path) . '/' . $recipeFolderName;
Expand Down Expand Up @@ -566,14 +569,8 @@

/**
* Get recipe file contents as an array
*
* @param File $file
*/
public function parseRecipeFile($file): ?array {
if (!$file) {
return null;
}

public function parseRecipeFile(File $file): ?array {
$json = json_decode($file->getContent(), true);

if (!$json) {
Expand All @@ -583,7 +580,7 @@
$json['id'] = $file->getParent()->getId();


if (!array_key_exists('dateCreated', $json) && method_exists($file, 'getCreationTime')) {
if (!array_key_exists('dateCreated', $json)) {
$json['dateCreated'] = $file->getCreationTime();
}

Expand All @@ -610,7 +607,7 @@

$recipe_folder = $recipe_folders[0];

// TODO: Check that file is really an image

Check warning on line 610 in lib/Service/RecipeService.php

View workflow job for this annotation

GitHub Actions / Check for added todo messages

Found TODO: Check that file is really an image
switch ($size) {
case 'full':
return $this->imageService->getImageAsFile($recipe_folder);
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="tests/psalm-baseline.xml"
findUnusedBaselineEntry="true"
phpVersion="8.0"
>
<!--
SPDX-FileCopyrightText: Christian Wolf <github@christianwolf.email>
Expand Down
Loading
Loading