Skip to content

Commit

Permalink
Fix PHP code styling
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Wolf <[email protected]>
  • Loading branch information
christianlupus committed Jul 6, 2022
1 parent 44e85bf commit 2303776
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 52 deletions.
18 changes: 13 additions & 5 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ public function getRules() : array {
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true
];
return array_merge(['@PSR12' => true], $parentRules, $additionalRules);
$ret = array_merge(['@PSR12' => true], $parentRules, $additionalRules);
// print_r($ret);
return $ret;
}
}

$config = new CookbookConfig();
$config
$finder = $config
->getFinder()
->ignoreVCSIgnored(true)
// ->ignoreVCSIgnored(true)
->exclude('build')
->exclude('l10n')
// ->notPath('lib/Vendor')
->exclude('src')
->exclude('node_modules')
->exclude('vendor')
->exclude('.github')
->in(__DIR__);
->in(__DIR__ );

$config->setFinder($finder);

// foreach($finder as $f) {
// print_r($f);
// }

// print_r($config);

return $config;

8 changes: 4 additions & 4 deletions lib/Db/RecipeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,13 @@ public function insertRecipes(array $recipes, string $userId) {
foreach ($recipes as $recipe) {
$qb->setParameter('id', $recipe['id'], $this->types->INT());
$qb->setParameter('name', $recipe['name'], $this->types->STRING());

$dateCreated = $this->parseDate($recipe['dateCreated']);
if($dateCreated === null) {
if ($dateCreated === null) {
$dateCreated = $this->parseDate('now');
}
$qb->setParameter('dateCreated', $dateCreated, $this->types->DATE());

$dateModified = $this->parseDate($recipe['dateModified']);
$qb->setParameter('dateModified', $dateModified, $this->types->DATE());

Expand Down Expand Up @@ -642,7 +642,7 @@ private function parseDate(?string $date) {
return null;
}

try{
try {
return new DateTimeImmutable($date);
} catch (\Exception $ex) {
return new DateTimeImmutable();
Expand Down
6 changes: 3 additions & 3 deletions lib/Helper/Filter/AbstractRecipeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

/**
* An abstract filter on a recipe.
*
*
* A filter should have a single purpose that is serves and implement this interface
*/
interface AbstractRecipeFilter {
/**
* Filter the given recipe according to the filter class specification.
*
*
* This function can make changes to the recipe array to carry out the needed changes.
* In order to signal if the JSON file needs updating, the return value must be true if and only if the recipe was changed.
*
* @param array $json The recipe data as array
* @param File $recipe The file containing the recipe for further processing
* @return boolean true, if and only if the recipe was changed
* @return bool true, if and only if the recipe was changed
* @throws InvalidRecipeException if the recipe was not correctly filtered
*/
public function apply(array &$json, File $recipe): bool;
Expand Down
37 changes: 19 additions & 18 deletions lib/Helper/Filter/DB/RecipeDatesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
namespace OCA\Cookbook\Helper\Filter\DB;

use DateTime;
use DateTimeImmutable;
use OCA\Cookbook\Helper\Filter\AbstractRecipeFilter;
use OCP\Files\File;

/**
* Ensure the dates of a recipe are valid
*
*
* This filter will update the recipe to have both valid dateCreated and dateModified.
* If the dates are given in correct format, nothing is changed.
*
*
* If only the dateModified is given, the dateCreated is set to the same value.
*
*
* If neither is given, the file modification time of the JSON file is taken into account.
*/
class RecipeDatesFilter implements AbstractRecipeFilter {

private const DATE_CREATED = 'dateCreated';
private const DATE_MODIFIED = 'dateModified';

Expand All @@ -28,15 +26,14 @@ class RecipeDatesFilter implements AbstractRecipeFilter {
/** @var string */
private $patternDate;

public function __construct()
{
public function __construct() {
$this->patternDate = join('|', [
'^' . self::PATTERN_DATE . '$',
'^' . self::PATTERN_DATE . 'T' . self::PATTERN_TIME . '$'
]);
}

public function apply(array &$json, File $recipe): bool {
public function apply(array &$json, File $recipe): bool {
$ret = false;

// First ensure the entries are present in general
Expand All @@ -47,7 +44,7 @@ public function apply(array &$json, File $recipe): bool {
$this->checkDateFormat($json, self::DATE_CREATED, $ret);
$this->checkDateFormat($json, self::DATE_MODIFIED, $ret);

if(is_null($json['dateCreated'])) {
if (is_null($json['dateCreated'])) {
if (is_null($json['dateModified'])) {
// No dates have been set. Fall back to time of file
$json['dateCreated'] = $this->getTimeFromFile($recipe);
Expand All @@ -67,45 +64,49 @@ public function apply(array &$json, File $recipe): bool {
return $ret;
}

/**
* Undocumented function
*
*/
private function getTimeFromFile(File $file): string {
$timestamp = $file->getCreationTime();
if($timestamp === 0) {
if ($timestamp === 0) {
$timestamp = $file->getUploadTime();
}
if($timestamp === 0) {
if ($timestamp === 0) {
$timestamp = $file->getMTime();
}

return $this->getDateFromTimestamp($timestamp);
}

private function getDateFromTimestamp(int $timestamp): string {
$date = new DateTime();
$date->setTimestamp($timestamp);

return $date->format(DateTime::ISO8601);
}

private function ensureEntryExists(array &$json, string $name, bool &$ret) {
if(!array_key_exists($name, $json)) {
if (!array_key_exists($name, $json)) {
$json[$name] = null;
$ret = true;
}
}

private function checkDateFormat(array &$json, string $name, bool &$ret) {
if($json[$name] === null) {
if ($json[$name] === null) {
return;
}

// Check for valid date format
if(preg_match('/' . $this->patternDate . '/', $json[$name]) === 1) {
if (preg_match('/' . $this->patternDate . '/', $json[$name]) === 1) {
return;
}

// Last desperate approach: Is it a timestamp?
if(preg_match('/^\d+$/', $json[$name])) {
if($json[$name] > 0) {
if (preg_match('/^\d+$/', $json[$name])) {
if ($json[$name] > 0) {
$json[$name] = $this->getDateFromTimestamp($json[$name]);
$ret = true;
return;
Expand Down
7 changes: 3 additions & 4 deletions lib/Migration/Version000000Date20220703174647.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* Auto-generated migration step: Please modify to your needs!
*/
class Version000000Date20220703174647 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
Expand All @@ -33,14 +32,14 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$schema = $schemaClosure();

$table = $schema->getTable('cookbook_names');
if(! $table->hasColumn('dateCreated')) {

if (! $table->hasColumn('dateCreated')) {
$table->addColumn('dateCreated', 'datetime_immutable', [
'notnull' => false,
]);
}

if(!$table->hasColumn('dateModified')) {
if (!$table->hasColumn('dateModified')) {
$table->addColumn('dateModified', 'datetime_immutable', [
'notnull' => false,
]);
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/DbCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function parseJSONFile(File $jsonFile): array {
$id = (int) $jsonFile->getParent()->getId();
$json['id'] = $id;

if(!isset($json['dateModified'])) {
if (!isset($json['dateModified'])) {
$json['dateModified'] = null;
}

Expand Down Expand Up @@ -226,11 +226,11 @@ private function isDbEntryUpToDate($id) {
if ($dbEntry['name'] !== $fileEntry['name']) {
return false;
}
if($dbEntry['dateCreated'] !== $fileEntry['dateCreated']) {
if ($dbEntry['dateCreated'] !== $fileEntry['dateCreated']) {
return false;
}

if($dbEntry['dateModified'] !== $fileEntry['dateModified']) {
if ($dbEntry['dateModified'] !== $fileEntry['dateModified']) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Service/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ public function getAllCategoriesInSearchIndex() {
*/
private function addDatesToRecipes(array &$recipes) {
foreach ($recipes as $i => $recipe) {
if(! array_key_exists('dateCreated', $recipe) || ! array_key_exists('dateModified', $recipe)) {
if (! array_key_exists('dateCreated', $recipe) || ! array_key_exists('dateModified', $recipe)) {
$r = $this->getRecipeById($recipe['recipe_id']);
$recipes[$i]['dateCreated'] = $r['dateCreated'];
$recipes[$i]['dateModified'] = $r['dateModified'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace OCA\Cookbook\tests\Migration\Setup\Migrations;

use OCP\DB\QueryBuilder\IQueryBuilder;

include_once __DIR__ . '/AbstractMigrationTestCase.php';

class Version000000Date20220703174647Test extends AbstractMigrationTestCase {
Expand Down Expand Up @@ -51,7 +49,7 @@ public function testRedundantEntriesInDB(/*$data, $updatedUsers*/) {
$this->assertEquals(2, count($row));
$this->assertNull($row['dateCreated']);
$this->assertNull($row['dateModified']);


//$this->assertEquals([null, null], $data[0]);
}
Expand Down
31 changes: 20 additions & 11 deletions tests/Unit/Helper/Filter/DB/RecipeDatesFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class RecipeDatesFilterTest extends TestCase {
/** @var RecipeDatesFilter */
private $dut;

protected function setUp(): void
{
protected function setUp(): void {
$this->dut = new RecipeDatesFilter();
}

Expand All @@ -29,6 +28,11 @@ public function dpFromJson() {

/**
* @dataProvider dpFromJson
* @param mixed $created
* @param mixed $modified
* @param mixed $expectedCreation
* @param mixed $expectedModification
* @param mixed $updated
*/
public function testFilterFromJson($created, $modified, $expectedCreation, $expectedModification, $updated) {
$recipe = [
Expand All @@ -53,7 +57,7 @@ public function testFilterFromJson($created, $modified, $expectedCreation, $expe

$this->assertEquals($copy, $recipe, 'Other entries must not change.');
}

public function dpFromFile() {
yield ['2022-07-06T09:08:54+0000', false, false, 1657098534, 1657098535, 1657098536];
yield ['2022-07-06T09:08:55+0000', false, false, 0, 1657098535, 1657098536];
Expand All @@ -63,39 +67,44 @@ public function dpFromFile() {

/**
* @dataProvider dpFromFile
* @param mixed $creation
* @param mixed $creationPresent
* @param mixed $modificationPresent
* @param mixed $creationTime
* @param mixed $uploadTime
* @param mixed $mTime
*/
public function testFilterFromFile($creation, $creationPresent, $modificationPresent, $creationTime, $uploadTime, $mTime) {
$recipe = [
'name' => 'my Recipe',
];
if($creationPresent) {
if ($creationPresent) {
$recipe['dateCreated'] = null;
}
if($modificationPresent) {
if ($modificationPresent) {
$recipe['dateModified'] = null;
}

$copy = $recipe;

/** @var Stub|File */
$file = $this->createStub(File::class);
$file->method('getCreationTime')->willReturn($creationTime);
$file->method('getUploadTime')->willReturn($uploadTime);
$file->method('getMTime')->willReturn($mTime);

$ret = $this->dut->apply($recipe, $file);

$this->assertTrue($ret, 'Reporting of modification status');

$this->assertEquals($creation, $recipe['dateCreated'], 'Wrong creation date');
$this->assertNull($recipe['dateModified'], 'Wrong modification date');

unset($recipe['dateCreated']);
unset($recipe['dateModified']);
unset($copy['dateCreated']);
unset($copy['dateModified']);

$this->assertEquals($copy, $recipe, 'Other entries must not change.');

$this->assertEquals($copy, $recipe, 'Other entries must not change.');
}
}

0 comments on commit 2303776

Please sign in to comment.