diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d9a058a36..75002190f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -195,16 +195,6 @@ parameters: count: 1 path: src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jQueryStore.php - - - message: "#^Cannot call method get\\(\\) on mixed\\.$#" - count: 1 - path: src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php - - - - message: "#^Method ProfessionalWiki\\\\NeoWiki\\\\GraphDatabasePlugins\\\\Neo4j\\\\Persistence\\\\Neo4jSubjectUpdater\\:\\:getNodeLabels\\(\\) should return array\\ but returns array\\\\.$#" - count: 1 - path: src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php - - message: "#^Cannot access property \\$page_title on array\\|stdClass\\|false\\.$#" count: 1 diff --git a/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jNodeLabels.php b/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jNodeLabels.php new file mode 100644 index 000000000..a90a81797 --- /dev/null +++ b/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jNodeLabels.php @@ -0,0 +1,52 @@ +run( + 'MATCH (n {id: $id}) RETURN labels(n) AS labels', + [ 'id' => $nodeId ] + ); + + if ( $result->isEmpty() ) { + return []; + } + + return $result->first()->get( 'labels' )->toArray(); + } + + /** + * @param string[] $labels + */ + public static function remove( TransactionInterface $transaction, string $nodeId, array $labels ): void { + if ( $labels === [] ) { + return; + } + + $transaction->run( + 'MATCH (n {id: $id}) REMOVE n:' . Cypher::buildLabelList( $labels ), + [ 'id' => $nodeId ] + ); + } + +} diff --git a/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStore.php b/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStore.php index 013cd796b..6450f969b 100644 --- a/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStore.php +++ b/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStore.php @@ -217,38 +217,11 @@ private function reduceSubjectToStub( TransactionInterface $transaction, Subject } private function removeNonStubLabels( TransactionInterface $transaction, SubjectId $subjectId ): void { - $labelsToRemove = array_diff( - $this->getSubjectLabels( $transaction, $subjectId ), - [ 'Subject' ] + Neo4jNodeLabels::remove( + $transaction, + $subjectId->text, + array_diff( Neo4jNodeLabels::read( $transaction, $subjectId->text ), [ 'Subject' ] ) ); - - if ( $labelsToRemove === [] ) { - return; - } - - $transaction->run( - 'MATCH (subject {id: $subjectId}) REMOVE subject:' . Cypher::buildLabelList( $labelsToRemove ), - [ 'subjectId' => $subjectId->text ] - ); - } - - /** - * @return string[] - */ - private function getSubjectLabels( TransactionInterface $transaction, SubjectId $subjectId ): array { - /** - * @var SummarizedResult $result - */ - $result = $transaction->run( - 'MATCH (subject {id: $subjectId}) RETURN labels(subject) AS labels', - [ 'subjectId' => $subjectId->text ] - ); - - if ( $result->isEmpty() ) { - return []; - } - - return $result->first()->get( 'labels' )->toArray(); } private function subjectHasIncomingRelations( TransactionInterface $transaction, SubjectId $subjectId ): bool { diff --git a/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php b/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php index 9d901d184..d752b2cec 100644 --- a/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php +++ b/src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php @@ -5,15 +5,12 @@ namespace ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence; use Laudis\Neo4j\Contracts\TransactionInterface; -use Laudis\Neo4j\Databags\SummarizedResult; -use Laudis\Neo4j\Types\CypherList; use ProfessionalWiki\NeoWiki\Application\SchemaLookup; use ProfessionalWiki\NeoWiki\Domain\Page\PageId; use ProfessionalWiki\NeoWiki\Domain\Schema\Schema; use ProfessionalWiki\NeoWiki\Domain\Statement; use ProfessionalWiki\NeoWiki\Domain\Subject\StatementList; use ProfessionalWiki\NeoWiki\Domain\Subject\Subject; -use ProfessionalWiki\NeoWiki\Domain\Subject\SubjectId; use Psr\Log\LoggerInterface; class Neo4jSubjectUpdater { @@ -124,17 +121,10 @@ private function updateHasSubjectRelation( Subject $subject, bool $isMainSubject } private function updateNodeLabels( Subject $subject ): void { - $oldLabels = $this->getNodeLabels( $subject->id ); + $oldLabels = Neo4jNodeLabels::read( $this->transaction, $subject->id->text ); $newLabels = [ 'Subject', $subject->getSchemaName()->getText() ]; - $labelsToRemove = array_diff( $oldLabels, $newLabels ); - - if ( $labelsToRemove !== [] ) { - $this->transaction->run( - 'MATCH (n {id: $id}) REMOVE n:' . Cypher::buildLabelList( $labelsToRemove ), - [ 'id' => $subject->id->text ] - ); - } + Neo4jNodeLabels::remove( $this->transaction, $subject->id->text, array_diff( $oldLabels, $newLabels ) ); $labelsToAdd = array_diff( $newLabels, $oldLabels ); @@ -146,30 +136,6 @@ private function updateNodeLabels( Subject $subject ): void { } } - /** - * @return string[] - */ - private function getNodeLabels( SubjectId $id ): array { - /** - * @var SummarizedResult $result - */ - $result = $this->transaction->run( - 'MATCH (n) WHERE n.id = $id RETURN labels(n)', - [ 'id' => $id->text ] - ); - - if ( $result->isEmpty() ) { - return []; - } - - /** - * @var CypherList $labels - */ - $labels = $result->first()->get( 'labels(n)' ); - - return $labels->toArray(); - } - private function updateRelations( Subject $subject, Schema $schema ): void { $updater = new Neo4jSubjectRelationUpdater( $subject->getId(), diff --git a/tests/phpunit/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStoreTest.php b/tests/phpunit/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStoreTest.php index 25b802b38..8243b04c2 100644 --- a/tests/phpunit/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStoreTest.php +++ b/tests/phpunit/GraphDatabasePlugins/Neo4j/Persistence/Neo4jProjectionStoreTest.php @@ -33,6 +33,7 @@ /** * @covers \ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence\Neo4jProjectionStore + * @covers \ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence\Neo4jNodeLabels * @group Database */ class Neo4jProjectionStoreTest extends NeoWikiIntegrationTestCase {