diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000..c2f8cb6 --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,20 @@ +name: "Linter" + +on: [pull_request] +jobs: + lint: + name: Linter + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - run: git checkout HEAD^2 + + - name: Run Linter + run: | + docker run --rm -v $PWD:/app composer sh -c \ + "composer install --profile --ignore-platform-reqs && composer lint" \ No newline at end of file diff --git a/composer.json b/composer.json index 72a150f..a359587 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,10 @@ "autoload-dev": { "psr-4": {"Utopia\\Tests\\": "tests/Transfer"} }, + "scripts": { + "lint": "./vendor/bin/pint --test", + "format": "./vendor/bin/pint" + }, "require": { "php": ">=8.0", "utopia-php/cli": "^0.13.0", diff --git a/playground.php b/playground.php index 1576e61..4aade02 100644 --- a/playground.php +++ b/playground.php @@ -5,7 +5,7 @@ * * A place to test and debug the Transfer Library stuff */ -require_once __DIR__ . '/vendor/autoload.php'; +require_once __DIR__.'/vendor/autoload.php'; use Appwrite\Query; use Dotenv\Dotenv; @@ -65,7 +65,7 @@ // $_ENV['DESTINATION_APPWRITE_TEST_KEY'] // ); -$destinationLocal = new Local(__DIR__ . '/localBackup/'); +$destinationLocal = new Local(__DIR__.'/localBackup/'); /** * Initialise Transfer Class diff --git a/src/Transfer/Cache.php b/src/Transfer/Cache.php index f9de689..f927f89 100644 --- a/src/Transfer/Cache.php +++ b/src/Transfer/Cache.php @@ -4,7 +4,7 @@ /** * Cache stores a local version of all data copied over from the source, This can be used as reference point for - * previous transfers and also help the destination to determine what needs to be updated, modified, + * previous transfers and also help the destination to determine what needs to be updated, modified, * added or removed. It is also used for debugging and validation purposes. */ class Cache @@ -18,15 +18,15 @@ public function __construct() /** * Add Resource - * + * * Places the resource in the cache, in the cache backend this also gets assigned a unique ID. - * - * @param Resource $resource + * + * @param resource $resource * @return void */ public function add($resource) { - if (!$resource->getInternalId()) { + if (! $resource->getInternalId()) { $resourceId = uniqid(); if (isset($this->cache[$resource->getName()][$resourceId])) { $resourceId = uniqid(); @@ -45,16 +45,16 @@ public function addAll(array $resources) /** * Update Resource - * + * * Updates the resource in the cache, if the resource does not exist in the cache an exception is thrown. * Use Add to add a new resource to the cache. - * - * @param Resource $resource + * + * @param resource $resource * @return void */ public function update($resource) { - if (!in_array($resource, $this->cache[$resource->getName()])) { + if (! in_array($resource, $this->cache[$resource->getName()])) { throw new \Exception('Resource does not exist in cache'); } @@ -70,15 +70,15 @@ public function updateAll($resources) /** * Remove Resource - * + * * Removes the resource from the cache, if the resource does not exist in the cache an exception is thrown. - * - * @param Resource $resource + * + * @param resource $resource * @return void */ public function remove($resource) { - if (!in_array($resource, $this->cache[$resource->getName()])) { + if (! in_array($resource, $this->cache[$resource->getName()])) { throw new \Exception('Resource does not exist in cache'); } @@ -112,9 +112,9 @@ public function getAll() /** * Wipe Cache - * + * * Removes all resources from the cache. - * + * * @return void */ public function wipe() diff --git a/src/Transfer/Destinations/Appwrite.php b/src/Transfer/Destinations/Appwrite.php index ca7ed54..daa1b05 100644 --- a/src/Transfer/Destinations/Appwrite.php +++ b/src/Transfer/Destinations/Appwrite.php @@ -15,12 +15,10 @@ use Utopia\Transfer\Resources\Auth\Membership; use Utopia\Transfer\Resources\Auth\User; use Utopia\Transfer\Resources\Database\Attribute; -use Utopia\Transfer\Resources\Database\Attributes\Boolean; use Utopia\Transfer\Resources\Database\Attributes\DateTime; +use Utopia\Transfer\Resources\Database\Attributes\Decimal; use Utopia\Transfer\Resources\Database\Attributes\Email; use Utopia\Transfer\Resources\Database\Attributes\Enum; -use Utopia\Transfer\Resources\Database\Attributes\Decimal; -use Utopia\Transfer\Resources\Database\Attributes\Integer; use Utopia\Transfer\Resources\Database\Attributes\IP; use Utopia\Transfer\Resources\Database\Attributes\Relationship; use Utopia\Transfer\Resources\Database\Attributes\Text; @@ -66,7 +64,7 @@ public static function getName(): string /** * Get Supported Resources */ - static function getSupportedResources(): array + public static function getSupportedResources(): array { return [ // Auth @@ -216,7 +214,7 @@ public function report(array $resources = []): array protected function import(array $resources, callable $callback): void { foreach ($resources as $resource) { - /** @var Resource $resource */ + /** @var resource $resource */ switch ($resource->getGroup()) { case Transfer::GROUP_DATABASES: $responseResource = $this->importDatabaseResource($resource); @@ -307,7 +305,7 @@ public function createAttribute(Attribute $attribute): void $databaseService->createStringAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getSize(), $attribute->getRequired(), $attribute->getDefault(), $attribute->getArray()); break; case Attribute::TYPE_INTEGER: - /** @var Integer $attribute */ + /** @var int $attribute */ $databaseService->createIntegerAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getRequired(), $attribute->getMin(), $attribute->getMax() ?? null, $attribute->getDefault(), $attribute->getArray()); break; case Attribute::TYPE_FLOAT: @@ -315,7 +313,7 @@ public function createAttribute(Attribute $attribute): void $databaseService->createFloatAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getRequired(), null, null, $attribute->getDefault(), $attribute->getArray()); break; case Attribute::TYPE_BOOLEAN: - /** @var Boolean $attribute */ + /** @var bool $attribute */ $databaseService->createBooleanAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getRequired(), $attribute->getDefault(), $attribute->getArray()); break; case Attribute::TYPE_DATETIME: @@ -412,7 +410,6 @@ public function importFileResource(Resource $resource): Resource /** * Import File Data * - * @param File $file * * @returns File */ @@ -501,7 +498,7 @@ public function importAuthResource(Resource $resource): Resource } if ($resource->getDisabled()) { - $userService->updateStatus($resource->getId(), !$resource->getDisabled()); + $userService->updateStatus($resource->getId(), ! $resource->getDisabled()); } break; @@ -525,7 +522,7 @@ public function importAuthResource(Resource $resource): Resource } } - public function importPasswordUser(User $user): array|null + public function importPasswordUser(User $user): ?array { $auth = new Users($this->client); $hash = $user->getPasswordHash(); @@ -544,7 +541,7 @@ public function importPasswordUser(User $user): array|null $hash->getSalt(), $hash->getSeparator(), $hash->getSigningKey(), - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; case Hash::ALGORITHM_BCRYPT: @@ -552,7 +549,7 @@ public function importPasswordUser(User $user): array|null $user->getId(), $user->getEmail(), $hash->getHash(), - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; case Hash::ALGORITHM_ARGON2: @@ -560,7 +557,7 @@ public function importPasswordUser(User $user): array|null $user->getId(), $user->getEmail(), $hash->getHash(), - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; case Hash::ALGORITHM_SHA256: @@ -569,7 +566,7 @@ public function importPasswordUser(User $user): array|null $user->getEmail(), $hash->getHash(), 'sha256', - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; case Hash::ALGORITHM_PHPASS: @@ -577,7 +574,7 @@ public function importPasswordUser(User $user): array|null $user->getId(), $user->getEmail(), $hash->getHash(), - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; case Hash::ALGORITHM_SCRYPT: @@ -590,7 +587,7 @@ public function importPasswordUser(User $user): array|null $hash->getPasswordMemory(), $hash->getPasswordParallel(), $hash->getPasswordLength(), - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; case Hash::ALGORITHM_PLAINTEXT: @@ -599,7 +596,7 @@ public function importPasswordUser(User $user): array|null $user->getEmail(), $user->getPhone(), $hash->getHash(), - empty($user->getUsername()) ? null : $user->getUsername() + empty($user->getUsername()) ? null : $user->getUsername() ); break; } diff --git a/src/Transfer/Destinations/Local.php b/src/Transfer/Destinations/Local.php index 73268ad..76a1428 100644 --- a/src/Transfer/Destinations/Local.php +++ b/src/Transfer/Destinations/Local.php @@ -24,10 +24,10 @@ public function __construct(string $path) { $this->path = $path; - if (!\file_exists($this->path)) { + if (! \file_exists($this->path)) { mkdir($this->path, 0777, true); - mkdir($this->path . '/files', 0777, true); - mkdir($this->path . '/deployments', 0777, true); + mkdir($this->path.'/files', 0777, true); + mkdir($this->path.'/deployments', 0777, true); } } @@ -42,7 +42,7 @@ public static function getName(): string /** * Get Supported Resources */ - static function getSupportedResources(): array + public static function getSupportedResources(): array { return [ Resource::TYPE_ATTRIBUTE, @@ -58,7 +58,7 @@ static function getSupportedResources(): array Resource::TYPE_INDEX, Resource::TYPE_TEAM, Resource::TYPE_MEMBERSHIP, - Resource::TYPE_USER + Resource::TYPE_USER, ]; } @@ -74,9 +74,9 @@ public function report(array $resources = []): array } // Check we can write to the file - if (!\is_writable($this->path . '/backup.json')) { - $report[Transfer::GROUP_DATABASES][] = 'Unable to write to file: ' . $this->path; - throw new \Exception('Unable to write to file: ' . $this->path); + if (! \is_writable($this->path.'/backup.json')) { + $report[Transfer::GROUP_DATABASES][] = 'Unable to write to file: '.$this->path; + throw new \Exception('Unable to write to file: '.$this->path); } return $report; @@ -93,13 +93,13 @@ private function sync(): void throw new \Exception('Unable to encode data to JSON, Are you accidentally encoding binary data?'); } - \file_put_contents($this->path . '/backup.json', \json_encode($this->data, JSON_PRETTY_PRINT)); + \file_put_contents($this->path.'/backup.json', \json_encode($this->data, JSON_PRETTY_PRINT)); } protected function import(array $resources, callable $callback): void { foreach ($resources as $resource) { - /** @var Resource $resource */ + /** @var resource $resource */ switch ($resource->getName()) { case 'Deployment': /** @var Deployment $resource */ @@ -107,7 +107,7 @@ protected function import(array $resources, callable $callback): void $this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray(); } - file_put_contents($this->path . 'deployments/' . $resource->getId() . '.tar.gz', $resource->getData(), FILE_APPEND); + file_put_contents($this->path.'deployments/'.$resource->getId().'.tar.gz', $resource->getData(), FILE_APPEND); break; case 'File': /** @var File $resource */ @@ -115,22 +115,22 @@ protected function import(array $resources, callable $callback): void // Handle folders if (str_contains($resource->getFileName(), '/')) { $folders = explode('/', $resource->getFileName()); - $folderPath = $this->path . '/files'; + $folderPath = $this->path.'/files'; foreach ($folders as $folder) { - $folderPath .= '/' . $folder; + $folderPath .= '/'.$folder; - if (!\file_exists($folderPath) && str_contains($folder, '.') === false) { + if (! \file_exists($folderPath) && str_contains($folder, '.') === false) { mkdir($folderPath, 0777, true); } } } - if ($resource->getStart() === 0 && \file_exists($this->path . '/files/' . $resource->getFileName())) { - unlink($this->path . '/files/' . $resource->getFileName()); + if ($resource->getStart() === 0 && \file_exists($this->path.'/files/'.$resource->getFileName())) { + unlink($this->path.'/files/'.$resource->getFileName()); } - file_put_contents($this->path . '/files/' . $resource->getFileName(), $resource->getData(), FILE_APPEND); + file_put_contents($this->path.'/files/'.$resource->getFileName(), $resource->getData(), FILE_APPEND); break; } diff --git a/src/Transfer/Resources/Auth/User.php b/src/Transfer/Resources/Auth/User.php index 782af79..14a2a2f 100644 --- a/src/Transfer/Resources/Auth/User.php +++ b/src/Transfer/Resources/Auth/User.php @@ -43,7 +43,7 @@ public function __construct( string $id = '', string $email = '', string $username = '', - ?Hash $passwordHash = null, + Hash $passwordHash = null, string $phone = '', array $types = [self::TYPE_ANONYMOUS], string $oauthProvider = '', diff --git a/src/Transfer/Resources/Database/Attributes/Boolean.php b/src/Transfer/Resources/Database/Attributes/Boolean.php index b72f531..427cdce 100644 --- a/src/Transfer/Resources/Database/Attributes/Boolean.php +++ b/src/Transfer/Resources/Database/Attributes/Boolean.php @@ -18,7 +18,7 @@ class Boolean extends Attribute /** * @param ?bool $default */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?bool $default = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, bool $default = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; diff --git a/src/Transfer/Resources/Database/Attributes/DateTime.php b/src/Transfer/Resources/Database/Attributes/DateTime.php index a8b4d5a..d00a619 100644 --- a/src/Transfer/Resources/Database/Attributes/DateTime.php +++ b/src/Transfer/Resources/Database/Attributes/DateTime.php @@ -12,7 +12,7 @@ class DateTime extends Attribute /** * @param ?string $default */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?string $default = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, string $default = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; diff --git a/src/Transfer/Resources/Database/Attributes/Decimal.php b/src/Transfer/Resources/Database/Attributes/Decimal.php index 9222646..2170f64 100644 --- a/src/Transfer/Resources/Database/Attributes/Decimal.php +++ b/src/Transfer/Resources/Database/Attributes/Decimal.php @@ -18,7 +18,7 @@ class Decimal extends Attribute * @param ?float $min * @param ?float $max */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?float $default = null, float $min = null, float $max = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, float $default = null, float $min = null, float $max = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; @@ -31,12 +31,12 @@ public function getTypeName(): string return Attribute::TYPE_FLOAT; } - public function getMin(): float|null + public function getMin(): ?float { return $this->min; } - public function getMax(): float|null + public function getMax(): ?float { return $this->max; } diff --git a/src/Transfer/Resources/Database/Attributes/Email.php b/src/Transfer/Resources/Database/Attributes/Email.php index 64b2df3..50970e1 100644 --- a/src/Transfer/Resources/Database/Attributes/Email.php +++ b/src/Transfer/Resources/Database/Attributes/Email.php @@ -12,7 +12,7 @@ class Email extends Attribute /** * @param ?string $default */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?string $default = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, string $default = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; diff --git a/src/Transfer/Resources/Database/Attributes/IP.php b/src/Transfer/Resources/Database/Attributes/IP.php index a204918..0ae58ca 100644 --- a/src/Transfer/Resources/Database/Attributes/IP.php +++ b/src/Transfer/Resources/Database/Attributes/IP.php @@ -12,7 +12,7 @@ class IP extends Attribute /** * @param string $default */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?string $default = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, string $default = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; diff --git a/src/Transfer/Resources/Database/Attributes/Integer.php b/src/Transfer/Resources/Database/Attributes/Integer.php index f84e2f5..de8379f 100644 --- a/src/Transfer/Resources/Database/Attributes/Integer.php +++ b/src/Transfer/Resources/Database/Attributes/Integer.php @@ -18,7 +18,7 @@ class Integer extends Attribute * @param ?int $min * @param ?int $max */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?int $default = null, int $min = null, int $max = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, int $default = null, int $min = null, int $max = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; @@ -31,24 +31,24 @@ public function getTypeName(): string return Attribute::TYPE_INTEGER; } - public function getMin(): int|null + public function getMin(): ?int { return $this->min; } - public function getMax(): int|null + public function getMax(): ?int { return $this->max; } - public function setMin(int|null $min): self + public function setMin(?int $min): self { $this->min = $min; return $this; } - public function setMax(int|null $max): self + public function setMax(?int $max): self { $this->max = $max; diff --git a/src/Transfer/Resources/Database/Attributes/Text.php b/src/Transfer/Resources/Database/Attributes/Text.php index 1b97d40..d41dadc 100644 --- a/src/Transfer/Resources/Database/Attributes/Text.php +++ b/src/Transfer/Resources/Database/Attributes/Text.php @@ -14,7 +14,7 @@ class Text extends Attribute /** * @param ?string $default */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?string $default = null, int $size = 256) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, string $default = null, int $size = 256) { parent::__construct($key, $collection, $required, $array); $this->default = $default; diff --git a/src/Transfer/Resources/Database/Attributes/URL.php b/src/Transfer/Resources/Database/Attributes/URL.php index 9e6ec41..8f4790e 100644 --- a/src/Transfer/Resources/Database/Attributes/URL.php +++ b/src/Transfer/Resources/Database/Attributes/URL.php @@ -12,7 +12,7 @@ class URL extends Attribute /** * @param ?string $default */ - public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?string $default = null) + public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, string $default = null) { parent::__construct($key, $collection, $required, $array); $this->default = $default; @@ -30,6 +30,6 @@ public function setDefault(string $default): void public function getTypeName(): string { - return Attribute::TYPE_URL;; + return Attribute::TYPE_URL; } } diff --git a/src/Transfer/Source.php b/src/Transfer/Source.php index e35a536..4a747aa 100644 --- a/src/Transfer/Source.php +++ b/src/Transfer/Source.php @@ -19,8 +19,8 @@ public function run(array $resources, callable $callback): void $this->transferCallback = function (array $returnedResources) use ($callback, $resources) { $prunedResurces = []; foreach ($returnedResources as $resource) { - /** @var Resource $resource */ - if (!in_array($resource->getName(), $resources)) { + /** @var resource $resource */ + if (! in_array($resource->getName(), $resources)) { $resource->setStatus(Resource::STATUS_SKIPPED); } else { $prunedResurces[] = $resource; @@ -37,7 +37,7 @@ public function run(array $resources, callable $callback): void /** * Export Resources * - * @param string[] $resources + * @param string[] $resources * @return void */ public function exportResources(array $resources, int $batchSize) @@ -83,7 +83,7 @@ public function exportResources(array $resources, int $batchSize) /** * Export Auth Group * - * @param array $resources Resources to export + * @param array $resources Resources to export * @return void */ abstract protected function exportGroupAuth(int $batchSize, array $resources); @@ -91,8 +91,8 @@ abstract protected function exportGroupAuth(int $batchSize, array $resources); /** * Export Databases Group * - * @param int $batchSize Max 100 - * @param array $resources Resources to export + * @param int $batchSize Max 100 + * @param array $resources Resources to export * @return void */ abstract protected function exportGroupDatabases(int $batchSize, array $resources); @@ -100,8 +100,8 @@ abstract protected function exportGroupDatabases(int $batchSize, array $resource /** * Export Storage Group * - * @param int $batchSize Max 5 - * @param array $resources Resources to export + * @param int $batchSize Max 5 + * @param array $resources Resources to export * @return void */ abstract protected function exportGroupStorage(int $batchSize, array $resources); @@ -109,8 +109,8 @@ abstract protected function exportGroupStorage(int $batchSize, array $resources) /** * Export Functions Group * - * @param int $batchSize Max 100 - * @param array $resources Resources to export + * @param int $batchSize Max 100 + * @param array $resources Resources to export * @return void */ abstract protected function exportGroupFunctions(int $batchSize, array $resources); diff --git a/src/Transfer/Sources/Appwrite.php b/src/Transfer/Sources/Appwrite.php index bede273..2de8dde 100644 --- a/src/Transfer/Sources/Appwrite.php +++ b/src/Transfer/Sources/Appwrite.php @@ -11,8 +11,8 @@ use Appwrite\Services\Users; use Utopia\Transfer\Resource; use Utopia\Transfer\Resources\Auth\Hash; -use Utopia\Transfer\Resources\Auth\Team; use Utopia\Transfer\Resources\Auth\Membership; +use Utopia\Transfer\Resources\Auth\Team; use Utopia\Transfer\Resources\Auth\User; use Utopia\Transfer\Resources\Database\Attribute; use Utopia\Transfer\Resources\Database\Attributes\Boolean; @@ -79,7 +79,7 @@ public static function getName(): string /** * Get Supported Resources */ - static function getSupportedResources(): array + public static function getSupportedResources(): array { return [ // Auth @@ -293,7 +293,7 @@ private function exportUsers(int $batchSize) '', $user['emailVerification'], $user['phoneVerification'], - !$user['status'], + ! $user['status'], $user['prefs'] ); @@ -473,7 +473,7 @@ private function convertAttribute(array $value, Collection $collection): Attribu { switch ($value['type']) { case 'string': - if (!isset($value['format'])) { + if (! isset($value['format'])) { return new Text( $value['key'], $collection, @@ -748,11 +748,11 @@ private function calculateTypes(array $user): array $types = []; - if (!empty($user['email'])) { + if (! empty($user['email'])) { $types[] = User::TYPE_EMAIL; } - if (!empty($user['phone'])) { + if (! empty($user['phone'])) { $types[] = User::TYPE_PHONE; } diff --git a/src/Transfer/Sources/Firebase.php b/src/Transfer/Sources/Firebase.php index c5a3f50..7f3597c 100644 --- a/src/Transfer/Sources/Firebase.php +++ b/src/Transfer/Sources/Firebase.php @@ -104,7 +104,7 @@ protected function call(string $method, string $path = '', array $headers = [], /** * Get Supported Resources */ - static function getSupportedResources(): array + public static function getSupportedResources(): array { return [ // Auth @@ -298,7 +298,7 @@ private function calculateArrayType(Collection $collection, string $key, array $ $previousType = null; foreach ($data['values'] as $field) { - if (!$previousType) { + if (! $previousType) { $previousType = $this->convertAttribute($collection, $key, $field); } elseif ($previousType->getName() != ($this->convertAttribute($collection, $key, $field))->getName()) { $isSameType = false; @@ -341,12 +341,12 @@ private function exportCollection(Collection $collection, int $batchSize, bool $ // Calculate Schema and handle subcollections $documentSchema = []; foreach ($result['documents'] as $document) { - if (!isset($document['fields'])) { + if (! isset($document['fields'])) { continue; //TODO: Transfer Empty Documents } foreach ($document['fields'] as $key => $field) { - if (!isset($documentSchema[$key])) { + if (! isset($documentSchema[$key])) { $documentSchema[$key] = $this->convertAttribute($collection, $key, $field); } } @@ -439,7 +439,7 @@ private function exportBuckets(int $batchsize) $this->callback([new Bucket($bucket['id'], [], false, $bucket['name'])]); } - if (!isset($result['nextPageToken'])) { + if (! isset($result['nextPageToken'])) { break; } @@ -469,7 +469,7 @@ private function exportFiles(int $batchsize) break; } - if (!isset($result['items'])) { + if (! isset($result['items'])) { break; } diff --git a/src/Transfer/Sources/NHost.php b/src/Transfer/Sources/NHost.php index e3f9b5e..d0ca19b 100644 --- a/src/Transfer/Sources/NHost.php +++ b/src/Transfer/Sources/NHost.php @@ -55,11 +55,11 @@ public function __construct(string $subdomain, string $region, string $adminSecr public function getDatabase(): PDO { - if (!$this->pdo) { + if (! $this->pdo) { try { - $this->pdo = new \PDO('pgsql:host=' . $this->subdomain . '.db.' . $this->region . '.nhost.run' . ';port=' . $this->port . ';dbname=' . $this->databaseName, $this->username, $this->password); + $this->pdo = new \PDO('pgsql:host='.$this->subdomain.'.db.'.$this->region.'.nhost.run'.';port='.$this->port.';dbname='.$this->databaseName, $this->username, $this->password); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database: ' . $e->getMessage()); + throw new \Exception('Failed to connect to database: '.$e->getMessage()); } } @@ -74,7 +74,7 @@ public static function getName(): string /** * Get Supported Resources */ - static function getSupportedResources(): array + public static function getSupportedResources(): array { return [ // Auth @@ -102,13 +102,13 @@ public function report(array $resources = []): array } try { - $db = $this->getDatabase(); + $db = $this->getDatabase(); } catch (\PDOException $e) { - throw new \Exception('Failed to connect to database. PDO Code: ' . $e->getCode() . ' Error: ' . $e->getMessage()); + throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage()); } - if (!empty($db->errorCode())) { - throw new \Exception('Failed to connect to database. PDO Code: ' . $db->errorCode() . (empty($db->errorInfo()[2]) ? '' : ' Error: ' . $db->errorInfo()[2])); + if (! empty($db->errorCode())) { + throw new \Exception('Failed to connect to database. PDO Code: '.$db->errorCode().(empty($db->errorInfo()[2]) ? '' : ' Error: '.$db->errorInfo()[2])); } // Auth @@ -117,7 +117,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access users table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access users table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_USER] = $statement->fetchColumn(); @@ -133,7 +133,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access tables table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access tables table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_COLLECTION] = $statement->fetchColumn(); @@ -144,7 +144,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access columns table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access columns table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_ATTRIBUTE] = $statement->fetchColumn(); @@ -155,7 +155,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access indexes table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access indexes table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_INDEX] = $statement->fetchColumn(); @@ -166,7 +166,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access tables table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access tables table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_DOCUMENT] = $statement->fetchColumn(); @@ -178,7 +178,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access buckets table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access buckets table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_BUCKET] = $statement->fetchColumn(); @@ -189,7 +189,7 @@ public function report(array $resources = []): array $statement->execute(); if ($statement->errorCode() !== '00000') { - throw new \Exception('Failed to access files table. Error: ' . $statement->errorInfo()[2]); + throw new \Exception('Failed to access files table. Error: '.$statement->errorInfo()[2]); } $report[Resource::TYPE_FILE] = $statement->fetchColumn(); @@ -363,12 +363,12 @@ private function exportDocuments(int $batchSize) $collections = $database->getCollections(); foreach ($collections as $collection) { - $total = $db->query('SELECT COUNT(*) FROM ' . $collection->getCollectionName())->fetchColumn(); + $total = $db->query('SELECT COUNT(*) FROM '.$collection->getCollectionName())->fetchColumn(); $offset = 0; while ($offset < $total) { - $statement = $db->prepare('SELECT row_to_json(t) FROM (SELECT * FROM ' . $collection->getCollectionName() . ' LIMIT :limit OFFSET :offset) t;'); + $statement = $db->prepare('SELECT row_to_json(t) FROM (SELECT * FROM '.$collection->getCollectionName().' LIMIT :limit OFFSET :offset) t;'); $statement->bindValue(':limit', $batchSize, \PDO::PARAM_INT); $statement->bindValue(':offset', $offset, \PDO::PARAM_INT); $statement->execute(); @@ -390,7 +390,7 @@ private function exportDocuments(int $batchSize) $processedData = []; foreach ($collectionAttributes as $attribute) { /** @var Attribute $attribute */ - if (!$attribute->getArray() && \is_array($data[$attribute->getKey()])) { + if (! $attribute->getArray() && \is_array($data[$attribute->getKey()])) { $processedData[$attribute->getKey()] = json_encode($data[$attribute->getKey()]); } else { $processedData[$attribute->getKey()] = $data[$attribute->getKey()]; @@ -411,7 +411,7 @@ private function convertAttribute(array $column, Collection $collection): Attrib $isArray = $column['data_type'] === 'ARRAY'; switch ($isArray ? str_replace('_', '', $column['udt_name']) : $column['data_type']) { - // Numbers + // Numbers case 'boolean': case 'bool': return new Boolean($column['column_name'], $collection, $column['is_nullable'] === 'NO', $isArray, $column['column_default']); @@ -527,11 +527,11 @@ private function calculateUserTypes(array $user): array $types = []; - if (!empty($user['password_hash'])) { + if (! empty($user['password_hash'])) { $types[] = User::TYPE_EMAIL; } - if (!empty($user['phone_number'])) { + if (! empty($user['phone_number'])) { $types[] = User::TYPE_PHONE; } @@ -632,7 +632,7 @@ private function exportFile(File $file) $end = Transfer::STORAGE_MAX_CHUNK_SIZE - 1; $fileSize = $file->getSize(); - $response = $this->call('GET', $url . "/v1/files/{$file->getId()}/presignedurl", [ + $response = $this->call('GET', $url."/v1/files/{$file->getId()}/presignedurl", [ 'X-Hasura-Admin-Secret' => $this->adminSecret, ]); diff --git a/src/Transfer/Sources/Supabase.php b/src/Transfer/Sources/Supabase.php index 4e0efc6..94833e6 100644 --- a/src/Transfer/Sources/Supabase.php +++ b/src/Transfer/Sources/Supabase.php @@ -193,7 +193,7 @@ 'application/x-zip-compressed' => 'zip', 'application/s-compressed' => 'zip', 'multipart/x-zip' => 'zip', - 'text/x-scriptzsh' => 'zsh' + 'text/x-scriptzsh' => 'zsh', ]; class Supabase extends NHost @@ -213,8 +213,7 @@ public static function getName(): string * * @return self */ - public function __construct(string $endpoint - , string $key, string $host, string $databaseName, string $username, string $password, string $port = '5432') + public function __construct(string $endpoint, string $key, string $host, string $databaseName, string $username, string $password, string $port = '5432') { $this->endpoint = $endpoint; $this->key = $key; @@ -247,7 +246,7 @@ public function report(array $resources = []): array throw new \Exception('Failed to connect to database. PDO Code: '.$e->getCode().' Error: '.$e->getMessage()); } - if (!empty($this->pdo->errorCode())) { + if (! empty($this->pdo->errorCode())) { throw new \Exception('Failed to connect to database. PDO Code: '.$this->pdo->errorCode().(empty($this->pdo->errorInfo()[2]) ? '' : ' Error: '.$this->pdo->errorInfo()[2])); } @@ -372,8 +371,8 @@ private function exportUsers(int $batchSize) $user['phone'] ?? '', $this->calculateAuthTypes($user), '', - !empty($user['email_confirmed_at']), - !empty($user['phone_confirmed_at']), + ! empty($user['email_confirmed_at']), + ! empty($user['phone_confirmed_at']), false, [] ); @@ -402,11 +401,11 @@ private function calculateAuthTypes(array $user): array $types = []; - if (!empty($user['encrypted_password'])) { + if (! empty($user['encrypted_password'])) { $types[] = User::TYPE_EMAIL; } - if (!empty($user['phone'])) { + if (! empty($user['phone'])) { $types[] = User::TYPE_PHONE; } diff --git a/src/Transfer/Target.php b/src/Transfer/Target.php index a6ac944..eb52827 100644 --- a/src/Transfer/Target.php +++ b/src/Transfer/Target.php @@ -35,7 +35,7 @@ abstract public static function getName(): string; /** * Get Supported Resources */ - abstract static function getSupportedResources(): array; + abstract public static function getSupportedResources(): array; /** * Register Cache @@ -71,7 +71,7 @@ abstract public function report(array $resources = []): array; protected function call(string $method, string $path = '', array $headers = [], array $params = []): array|string { $headers = array_merge($this->headers, $headers); - $ch = curl_init((str_contains($path, 'http') ? $path.(($method == 'GET' && !empty($params)) ? '?'.http_build_query($params) : '') : $this->endpoint.$path.(($method == 'GET' && !empty($params)) ? '?'.http_build_query($params) : ''))); + $ch = curl_init((str_contains($path, 'http') ? $path.(($method == 'GET' && ! empty($params)) ? '?'.http_build_query($params) : '') : $this->endpoint.$path.(($method == 'GET' && ! empty($params)) ? '?'.http_build_query($params) : ''))); $responseHeaders = []; $responseStatus = -1; $responseType = ''; diff --git a/src/Transfer/Transfer.php b/src/Transfer/Transfer.php index 16f091b..715fb02 100644 --- a/src/Transfer/Transfer.php +++ b/src/Transfer/Transfer.php @@ -77,8 +77,8 @@ public function getStatusCounters() foreach ($this->cache->getAll() as $resources) { foreach ($resources as $resource) { - /** @var Resource $resource */ - if (!array_key_exists($resource->getName(), $status)) { + /** @var resource $resource */ + if (! array_key_exists($resource->getName(), $status)) { $status[$resource->getName()] = [ Resource::STATUS_PENDING => 0, Resource::STATUS_SUCCESS => 0, diff --git a/tests/Transfer/E2E/Adapters/Mock.php b/tests/Transfer/E2E/Adapters/Mock.php index b0de6bf..caa7bfa 100644 --- a/tests/Transfer/E2E/Adapters/Mock.php +++ b/tests/Transfer/E2E/Adapters/Mock.php @@ -1,4 +1,5 @@ getName()) { case 'Deployment': /** @var Deployment $resource */ diff --git a/tests/Transfer/E2E/Sources/Base.php b/tests/Transfer/E2E/Sources/Base.php index e1d06ef..e7605e7 100644 --- a/tests/Transfer/E2E/Sources/Base.php +++ b/tests/Transfer/E2E/Sources/Base.php @@ -4,7 +4,6 @@ use PHPUnit\Framework\TestCase; use Utopia\Tests\E2E\Adapters\Mock; -use Utopia\Tests\E2E\Adapters\MockDestination; use Utopia\Transfer\Destination; use Utopia\Transfer\Resource; use Utopia\Transfer\Source; @@ -13,13 +12,16 @@ abstract class Base extends TestCase { protected ?Transfer $transfer = null; + protected ?Source $source = null; + protected ?Destination $destination = null; public function __construct() { - if (!$this->source) + if (! $this->source) { throw new \Exception('Source not set'); + } $this->destination = new Mock(); $this->transfer = new Transfer($this->source, $this->destination); diff --git a/tests/Transfer/E2E/Sources/NHostTest.php b/tests/Transfer/E2E/Sources/NHostTest.php index 700478f..0b6586a 100644 --- a/tests/Transfer/E2E/Sources/NHostTest.php +++ b/tests/Transfer/E2E/Sources/NHostTest.php @@ -2,16 +2,18 @@ namespace Utopia\Tests\E2E\Sources; -use Utopia\Transfer\Sources\NHost; -use Utopia\Transfer\Transfer; use Utopia\Tests\E2E\Adapters\Mock; use Utopia\Transfer\Destination; use Utopia\Transfer\Source; +use Utopia\Transfer\Sources\NHost; +use Utopia\Transfer\Transfer; class NHostTest extends Base { protected ?Source $source = null; + protected ?Transfer $transfer = null; + protected ?Destination $destination = null; public function __construct() diff --git a/tests/e2e/adapters/Mock.php b/tests/e2e/adapters/Mock.php index 3c6c554..5d1aad3 100644 --- a/tests/e2e/adapters/Mock.php +++ b/tests/e2e/adapters/Mock.php @@ -12,7 +12,7 @@ public static function getName(): string return 'MockSource'; } - static function getSupportedResources(): array + public static function getSupportedResources(): array { return [ Transfer::GROUP_AUTH, @@ -31,47 +31,47 @@ public function report(array $groups = []): array /** * Export Auth Group * - * @param array $resources Resources to export + * @param array $resources Resources to export * @return void */ protected function exportGroupAuth(int $batchSize, array $resources) { - + } /** * Export Databases Group * - * @param int $batchSize Max 100 - * @param array $resources Resources to export + * @param int $batchSize Max 100 + * @param array $resources Resources to export * @return void */ protected function exportGroupDatabases(int $batchSize, array $resources) { - + } /** * Export Storage Group * - * @param int $batchSize Max 5 - * @param array $resources Resources to export + * @param int $batchSize Max 5 + * @param array $resources Resources to export * @return void */ protected function exportGroupStorage(int $batchSize, array $resources) { - + } /** * Export Functions Group * - * @param int $batchSize Max 100 - * @param array $resources Resources to export + * @param int $batchSize Max 100 + * @param array $resources Resources to export * @return void */ protected function exportGroupFunctions(int $batchSize, array $resources) { - + } }