Skip to content

Commit beae168

Browse files
committed
fix(ORM): Address feedback from review
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent 4fe3780 commit beae168

15 files changed

Lines changed: 89 additions & 49 deletions

File tree

apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
use OCP\AppFramework\Db\DoesNotExistException;
1313
use OCP\AppFramework\ORM\Repository;
1414
use OCP\DB\QueryBuilder\IQueryBuilder;
15-
use OCP\IDBConnection;
1615
use OCP\IUser;
1716

1817
/**
1918
* @template-extends Repository<BackupCode>
2019
*/
2120
class BackupCodeMapper extends Repository {
22-
public function __construct(IDBConnection $db) {
23-
parent::__construct($db, BackupCode::class);
24-
}
21+
public const string entityClass = BackupCode::class;
2522

2623
/**
2724
* @return \Generator<BackupCode>

apps/twofactor_backupcodes/tests/Unit/Service/BackupCodeStorageTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ public function testGetBackupCodeDisabled(): void {
124124
$this->mapper->expects($this->once())
125125
->method('findByUser')
126126
->with($user)
127-
->willReturnCallback(function () { if (false) { yield true; }});
127+
->willReturnCallback(function () {
128+
if (false) {
129+
yield true;
130+
}
131+
});
128132

129133
$expected = [
130134
'enabled' => false,

lib/private/AppFramework/ORM/EntityManager.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class EntityManager {
2323
public function __construct(
24-
readonly private IDBConnection $connection,
24+
private readonly IDBConnection $connection,
2525
) {
2626
}
2727

@@ -43,12 +43,17 @@ public function getEntityInfo(string $entityClass): EntityInfo {
4343
}
4444

4545
/**
46+
* Generic, runtime-typed repository factory for callers that only have the entity class as
47+
* a value (e.g. tests). Hand-written repositories (e.g. BackupCodeMapper) should instead
48+
* extend Repository and override its `entityClass` constant, which also gets them proper
49+
* static analysis of their entity type.
50+
*
4651
* @template T of object
4752
* @param class-string<T> $entityClass
4853
* @return Repository<T>
4954
*/
5055
public function getRepository(string $entityClass): Repository {
51-
return new Repository($this->connection, $entityClass);
56+
return new Repository($this->connection, $this, $entityClass);
5257
}
5358

5459
/**

lib/private/Tagging/Tag.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use OCP\AppFramework\ORM\Attribute\Entity;
1313
use OCP\AppFramework\ORM\Attribute\Id;
1414
use OCP\DB\Types;
15-
use OCP\Snowflake\ISnowflakeGenerator;
1615

1716
/**
1817
* Class to represent a tag.

lib/private/Tagging/TagMapper.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
namespace OC\Tagging;
1010

1111
use OCP\AppFramework\ORM\Repository;
12-
use OCP\IDBConnection;
1312

1413
/**
1514
* Mapper for Tag entity
1615
*
1716
* @template-extends Repository<Tag>
1817
*/
1918
class TagMapper extends Repository {
20-
public function __construct(IDBConnection $db) {
21-
parent::__construct($db, Tag::class);
22-
}
19+
public const string entityClass = Tag::class;
2320

2421
/**
2522
* Load tags from the database.

lib/private/Tags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ private function getTagId(string $name): int|false {
584584
*
585585
* @param string $name The tag name.
586586
* @return integer|false The tag object's offset within the $this->tags
587-
* array or false if it doesn't exist.
587+
* array or false if it doesn't exist.
588588
*/
589589
private function getTagByName(string $name): int|false {
590590
return $this->array_searchi($name, $this->tags);

lib/public/AppFramework/ORM/Attribute/Column.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#[Attribute(Attribute::TARGET_PROPERTY)]
2828
#[Consumable(since: '35.0.0')]
2929
final readonly class Column {
30+
/** @since 35.0.0 */
3031
public function __construct(
3132
/** @param non-empty-string $name The name of the column in the database. */
3233
public string $name,

lib/public/AppFramework/ORM/Attribute/Entity.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
#[Attribute(Attribute::TARGET_CLASS)]
2727
#[Consumable(since: '35.0.0')]
2828
final readonly class Entity {
29+
/** @since 35.0.0 */
2930
public function __construct(
30-
/** @param non-empty-string $name */
31+
/** @param non-empty-string $name The name of the table in the database. */
3132
public string $name,
3233
) {
3334
}

lib/public/AppFramework/ORM/Attribute/Id.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#[Attribute(Attribute::TARGET_PROPERTY)]
2929
#[Consumable(since: '35.0.0')]
3030
final readonly class Id {
31+
/** @since 35.0.0 */
3132
public function __construct(
3233
/** @param class-string<ISnowflakeGenerator> $generatorClass */
3334
public ?string $generatorClass = null,

lib/public/AppFramework/ORM/Attribute/JoinColumn.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#[Attribute(Attribute::TARGET_PROPERTY)]
2828
#[Consumable(since: '35.0.0')]
2929
final readonly class JoinColumn {
30+
/** @since 35.0.0 */
3031
public function __construct(
3132
/** @param non-empty-string $name The name of the column in the database. */
3233
public string $name,

0 commit comments

Comments
 (0)