Skip to content

Commit

Permalink
EZP-31103: Introduced strict types for ContentTypeService
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Nov 15, 2019
1 parent 1da1ad9 commit c6b76e2
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 228 deletions.
71 changes: 34 additions & 37 deletions eZ/Publish/API/Repository/ContentTypeService.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* File containing the eZ\Publish\API\Repository\ContentTypeService class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository;

use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
Expand All @@ -20,9 +20,6 @@
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupUpdateStruct;
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupCreateStruct;

/**
* @example Examples/contenttype.php
*/
interface ContentTypeService
{
/**
Expand All @@ -35,19 +32,19 @@ interface ContentTypeService
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup
*/
public function createContentTypeGroup(ContentTypeGroupCreateStruct $contentTypeGroupCreateStruct);
public function createContentTypeGroup(ContentTypeGroupCreateStruct $contentTypeGroupCreateStruct): ContentTypeGroup;

/**
* Get a Content Type Group object by id.
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If group can not be found
*
* @param mixed $contentTypeGroupId
* @param int $contentTypeGroupId
* @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup
*/
public function loadContentTypeGroup($contentTypeGroupId, array $prioritizedLanguages = []);
public function loadContentTypeGroup(int $contentTypeGroupId, array $prioritizedLanguages = []): ContentTypeGroup;

/**
* Get a Content Type Group object by identifier.
Expand All @@ -59,7 +56,7 @@ public function loadContentTypeGroup($contentTypeGroupId, array $prioritizedLang
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup
*/
public function loadContentTypeGroupByIdentifier($contentTypeGroupIdentifier, array $prioritizedLanguages = []);
public function loadContentTypeGroupByIdentifier(string $contentTypeGroupIdentifier, array $prioritizedLanguages = []): ContentTypeGroup;

/**
* Get all Content Type Groups.
Expand All @@ -68,7 +65,7 @@ public function loadContentTypeGroupByIdentifier($contentTypeGroupIdentifier, ar
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup[]
*/
public function loadContentTypeGroups(array $prioritizedLanguages = []);
public function loadContentTypeGroups(array $prioritizedLanguages = []): iterable;

/**
* Update a Content Type Group object.
Expand All @@ -79,7 +76,7 @@ public function loadContentTypeGroups(array $prioritizedLanguages = []);
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup $contentTypeGroup the content type group to be updated
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupUpdateStruct $contentTypeGroupUpdateStruct
*/
public function updateContentTypeGroup(ContentTypeGroup $contentTypeGroup, ContentTypeGroupUpdateStruct $contentTypeGroupUpdateStruct);
public function updateContentTypeGroup(ContentTypeGroup $contentTypeGroup, ContentTypeGroupUpdateStruct $contentTypeGroupUpdateStruct): void;

/**
* Delete a Content Type Group.
Expand All @@ -91,7 +88,7 @@ public function updateContentTypeGroup(ContentTypeGroup $contentTypeGroup, Conte
*
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup $contentTypeGroup
*/
public function deleteContentTypeGroup(ContentTypeGroup $contentTypeGroup);
public function deleteContentTypeGroup(ContentTypeGroup $contentTypeGroup): void;

/**
* Create a Content Type object.
Expand All @@ -112,19 +109,19 @@ public function deleteContentTypeGroup(ContentTypeGroup $contentTypeGroup);
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft
*/
public function createContentType(ContentTypeCreateStruct $contentTypeCreateStruct, array $contentTypeGroups);
public function createContentType(ContentTypeCreateStruct $contentTypeCreateStruct, array $contentTypeGroups): ContentTypeDraft;

/**
* Get a Content Type object by id.
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If a content type with the given id and status DEFINED can not be found
*
* @param mixed $contentTypeId
* @param int $contentTypeId
* @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*/
public function loadContentType($contentTypeId, array $prioritizedLanguages = []);
public function loadContentType(int $contentTypeId, array $prioritizedLanguages = []): ContentType;

/**
* Get a Content Type object by identifier.
Expand All @@ -136,7 +133,7 @@ public function loadContentType($contentTypeId, array $prioritizedLanguages = []
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*/
public function loadContentTypeByIdentifier($identifier, array $prioritizedLanguages = []);
public function loadContentTypeByIdentifier(string $identifier, array $prioritizedLanguages = []): ContentType;

/**
* Get a Content Type object by id.
Expand All @@ -148,7 +145,7 @@ public function loadContentTypeByIdentifier($identifier, array $prioritizedLangu
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*/
public function loadContentTypeByRemoteId($remoteId, array $prioritizedLanguages = []);
public function loadContentTypeByRemoteId(string $remoteId, array $prioritizedLanguages = []): ContentType;

/**
* Get a Content Type object draft by id.
Expand All @@ -160,7 +157,7 @@ public function loadContentTypeByRemoteId($remoteId, array $prioritizedLanguages
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft
*/
public function loadContentTypeDraft($contentTypeId, bool $ignoreOwnership = false);
public function loadContentTypeDraft(int $contentTypeId, bool $ignoreOwnership = false): ContentTypeDraft;

/**
* Bulk-load Content Type objects by ids.
Expand All @@ -169,7 +166,7 @@ public function loadContentTypeDraft($contentTypeId, bool $ignoreOwnership = fal
*
* @since 7.3
*
* @param mixed[] $contentTypeIds
* @param int[] $contentTypeIds
* @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType[]|iterable
Expand All @@ -184,7 +181,7 @@ public function loadContentTypeList(array $contentTypeIds, array $prioritizedLan
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType[] an array of {@link ContentType} which have status DEFINED
*/
public function loadContentTypes(ContentTypeGroup $contentTypeGroup, array $prioritizedLanguages = []);
public function loadContentTypes(ContentTypeGroup $contentTypeGroup, array $prioritizedLanguages = []): iterable;

/**
* Creates a draft from an existing content type.
Expand All @@ -199,7 +196,7 @@ public function loadContentTypes(ContentTypeGroup $contentTypeGroup, array $prio
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft
*/
public function createContentTypeDraft(ContentType $contentType);
public function createContentTypeDraft(ContentType $contentType): ContentTypeDraft;

/**
* Update a Content Type object.
Expand All @@ -212,7 +209,7 @@ public function createContentTypeDraft(ContentType $contentType);
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft $contentTypeDraft
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeUpdateStruct $contentTypeUpdateStruct
*/
public function updateContentTypeDraft(ContentTypeDraft $contentTypeDraft, ContentTypeUpdateStruct $contentTypeUpdateStruct);
public function updateContentTypeDraft(ContentTypeDraft $contentTypeDraft, ContentTypeUpdateStruct $contentTypeUpdateStruct): void;

/**
* Delete a Content Type object.
Expand All @@ -226,7 +223,7 @@ public function updateContentTypeDraft(ContentTypeDraft $contentTypeDraft, Conte
*
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
*/
public function deleteContentType(ContentType $contentType);
public function deleteContentType(ContentType $contentType): void;

/**
* Copy Type incl fields and groupIds to a new Type object.
Expand All @@ -241,7 +238,7 @@ public function deleteContentType(ContentType $contentType);
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*/
public function copyContentType(ContentType $contentType, User $creator = null);
public function copyContentType(ContentType $contentType, User $creator = null): ContentType;

/**
* Assigns a content type to a content type group.
Expand All @@ -252,7 +249,7 @@ public function copyContentType(ContentType $contentType, User $creator = null);
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup $contentTypeGroup
*/
public function assignContentTypeGroup(ContentType $contentType, ContentTypeGroup $contentTypeGroup);
public function assignContentTypeGroup(ContentType $contentType, ContentTypeGroup $contentTypeGroup): void;

/**
* Unassign a content type from a group.
Expand All @@ -264,7 +261,7 @@ public function assignContentTypeGroup(ContentType $contentType, ContentTypeGrou
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup $contentTypeGroup
*/
public function unassignContentTypeGroup(ContentType $contentType, ContentTypeGroup $contentTypeGroup);
public function unassignContentTypeGroup(ContentType $contentType, ContentTypeGroup $contentTypeGroup): void;

/**
* Adds a new field definition to an existing content type.
Expand All @@ -283,7 +280,7 @@ public function unassignContentTypeGroup(ContentType $contentType, ContentTypeGr
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft $contentTypeDraft
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct $fieldDefinitionCreateStruct
*/
public function addFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefinitionCreateStruct $fieldDefinitionCreateStruct);
public function addFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefinitionCreateStruct $fieldDefinitionCreateStruct): void;

/**
* Remove a field definition from an existing Type.
Expand All @@ -294,7 +291,7 @@ public function addFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefi
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft $contentTypeDraft
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition
*/
public function removeFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefinition $fieldDefinition);
public function removeFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefinition $fieldDefinition): void;

/**
* Update a field definition.
Expand All @@ -307,7 +304,7 @@ public function removeFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldD
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition the field definition which should be updated
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct $fieldDefinitionUpdateStruct
*/
public function updateFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefinition $fieldDefinition, FieldDefinitionUpdateStruct $fieldDefinitionUpdateStruct);
public function updateFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldDefinition $fieldDefinition, FieldDefinitionUpdateStruct $fieldDefinitionUpdateStruct): void;

/**
* Publish the content type and update content objects.
Expand All @@ -320,7 +317,7 @@ public function updateFieldDefinition(ContentTypeDraft $contentTypeDraft, FieldD
*
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft $contentTypeDraft
*/
public function publishContentTypeDraft(ContentTypeDraft $contentTypeDraft);
public function publishContentTypeDraft(ContentTypeDraft $contentTypeDraft): void;

/**
* Instantiates a new content type group create class.
Expand All @@ -329,7 +326,7 @@ public function publishContentTypeDraft(ContentTypeDraft $contentTypeDraft);
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupCreateStruct
*/
public function newContentTypeGroupCreateStruct($identifier);
public function newContentTypeGroupCreateStruct(string $identifier): ContentTypeGroupCreateStruct;

/**
* Instantiates a new content type create class.
Expand All @@ -338,21 +335,21 @@ public function newContentTypeGroupCreateStruct($identifier);
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeCreateStruct
*/
public function newContentTypeCreateStruct($identifier);
public function newContentTypeCreateStruct(string $identifier): ContentTypeCreateStruct;

/**
* Instantiates a new content type update struct.
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeUpdateStruct
*/
public function newContentTypeUpdateStruct();
public function newContentTypeUpdateStruct(): ContentTypeUpdateStruct;

/**
* Instantiates a new content type update struct.
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroupUpdateStruct
*/
public function newContentTypeGroupUpdateStruct();
public function newContentTypeGroupUpdateStruct(): ContentTypeGroupUpdateStruct;

/**
* Instantiates a field definition create struct.
Expand All @@ -362,14 +359,14 @@ public function newContentTypeGroupUpdateStruct();
*
* @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct
*/
public function newFieldDefinitionCreateStruct($identifier, $fieldTypeIdentifier);
public function newFieldDefinitionCreateStruct(string $identifier, string $fieldTypeIdentifier): FieldDefinitionCreateStruct;

/**
* Instantiates a field definition update class.
*
* @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct
*/
public function newFieldDefinitionUpdateStruct();
public function newFieldDefinitionUpdateStruct(): FieldDefinitionUpdateStruct;

/**
* Returns true if the given content type $contentType has content instances.
Expand All @@ -380,7 +377,7 @@ public function newFieldDefinitionUpdateStruct();
*
* @return bool
*/
public function isContentTypeUsed(ContentType $contentType);
public function isContentTypeUsed(ContentType $contentType): bool;

/**
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft $contentTypeDraft
Expand Down
8 changes: 4 additions & 4 deletions eZ/Publish/Core/Event/ContentTypeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
}

public function createContentTypeGroup(ContentTypeGroupCreateStruct $contentTypeGroupCreateStruct)
public function createContentTypeGroup(ContentTypeGroupCreateStruct $contentTypeGroupCreateStruct): ContentTypeGroup
{
$eventData = [$contentTypeGroupCreateStruct];

Expand Down Expand Up @@ -133,7 +133,7 @@ public function deleteContentTypeGroup(ContentTypeGroup $contentTypeGroup): void
public function createContentType(
ContentTypeCreateStruct $contentTypeCreateStruct,
array $contentTypeGroups
) {
): ContentTypeDraft {
$eventData = [
$contentTypeCreateStruct,
$contentTypeGroups,
Expand All @@ -157,7 +157,7 @@ public function createContentType(
return $contentTypeDraft;
}

public function createContentTypeDraft(ContentType $contentType)
public function createContentTypeDraft(ContentType $contentType): ContentTypeDraft
{
$eventData = [$contentType];

Expand Down Expand Up @@ -223,7 +223,7 @@ public function deleteContentType(ContentType $contentType): void
public function copyContentType(
ContentType $contentType,
User $creator = null
) {
): ContentType {
$eventData = [
$contentType,
$creator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class ContentExtensionTest extends FileSystemTwigIntegrationTestCase
/** @var \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject */
private $fieldHelperMock;

/** @var \eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition[] */
private $fieldDefinitions = [];

/** @var int[] */
private $identityMap = [];

public function getExtensions()
{
$this->fieldHelperMock = $this->createMock(FieldHelper::class);
Expand Down Expand Up @@ -68,14 +72,20 @@ public function getFixturesDir()
*
* @return Content
*/
protected function getContent($contentTypeIdentifier, array $fieldsData, array $namesData = [])
protected function getContent(string $contentTypeIdentifier, array $fieldsData, array $namesData = [])
{
if (!array_key_exists($contentTypeIdentifier, $this->identityMap)) {
$this->identityMap[$contentTypeIdentifier] = count($this->identityMap) + 1;
}

$contentTypeId = $this->identityMap[$contentTypeIdentifier];

$fields = [];
foreach ($fieldsData as $fieldTypeIdentifier => $fieldsArray) {
$fieldsArray = isset($fieldsArray['id']) ? [$fieldsArray] : $fieldsArray;
foreach ($fieldsArray as $fieldInfo) {
// Save field definitions in property for mocking purposes
$this->fieldDefinitions[$contentTypeIdentifier][$fieldInfo['fieldDefIdentifier']] = new FieldDefinition(
$this->fieldDefinitions[$contentTypeId][$fieldInfo['fieldDefIdentifier']] = new FieldDefinition(
[
'identifier' => $fieldInfo['fieldDefIdentifier'],
'id' => $fieldInfo['id'],
Expand All @@ -101,7 +111,7 @@ protected function getContent($contentTypeIdentifier, array $fieldsData, array $
'id' => 42,
'mainLanguageCode' => 'fre-FR',
// Using as id as we don't really care to test the service here
'contentTypeId' => $contentTypeIdentifier,
'contentTypeId' => $contentTypeId,
]
),
]
Expand Down
Loading

0 comments on commit c6b76e2

Please sign in to comment.