Skip to content

Commit

Permalink
modernize php code
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Jan 15, 2024
1 parent 7c4e151 commit a5c4988
Show file tree
Hide file tree
Showing 52 changed files with 321 additions and 496 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/PHPCR/DocumentClassMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private function expandClassName(DocumentManagerInterface $dm, string $className
return null;
}

if (false !== strpos($className, ':')) {
if (str_contains($className, ':')) {
$className = $dm->getClassMetadata($className)->getName();
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/ODM/PHPCR/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class DocumentManager implements DocumentManagerInterface
/**
* @var TranslationStrategyInterface[]
*/
protected array $translationStrategy;
private array $translationStrategy;

protected LocaleChooserInterface $localeChooserStrategy;
private LocaleChooserInterface $localeChooserStrategy;
private ValueConverter $valueConverter;

public function __construct(SessionInterface $session, Configuration $config = null, EventManager $evm = null)
Expand Down Expand Up @@ -182,7 +182,7 @@ public function find(?string $className, $id): ?object
} catch (ItemNotFoundException $e) {
return null;
}
} elseif (0 !== strpos($id, '/')) {
} elseif (!str_starts_with($id, '/')) {
$id = '/'.$id;
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public function findMany(?string $className, array $ids): Collection
foreach ($ids as $key => $id) {
if (UUIDHelper::isUUID($id)) {
$uuids[$id] = $key;
} elseif (0 !== strpos($id, '/')) {
} elseif (!str_starts_with($id, '/')) {
$ids[$key] = '/'.$id;
}
}
Expand Down Expand Up @@ -257,7 +257,7 @@ public function findTranslation(?string $className, string $id, string $locale,
} catch (ItemNotFoundException $e) {
return null;
}
} elseif (0 !== strpos($id, '/')) {
} elseif (!str_starts_with($id, '/')) {
$id = '/'.$id;
}

Expand Down Expand Up @@ -424,7 +424,7 @@ public function isDocumentTranslatable(object $document): bool

public function move(object $document, string $targetPath): void
{
if (0 !== strpos($targetPath, '/')) {
if (!str_starts_with($targetPath, '/')) {
$targetPath = '/'.$targetPath;
}

Expand Down
11 changes: 4 additions & 7 deletions lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ClassMetadata implements ClassMetadataInterface
*/
public const GENERATOR_TYPE_AUTO = 4;

protected static $validVersionableMappings = ['simple', 'full'];
private static $validVersionableMappings = ['simple', 'full'];

/**
* READ-ONLY: The ReflectionProperty instances of the mapped class.
Expand Down Expand Up @@ -837,7 +837,7 @@ public function mapLifecycleCallbacks(array $mapping): void
*
* @throws MappingException
*/
protected function validateAndCompleteFieldMapping(array $mapping, self $inherited = null, bool $isField = true, $phpcrLabel = 'property'): array
private function validateAndCompleteFieldMapping(array $mapping, self $inherited = null, bool $isField = true, $phpcrLabel = 'property'): array
{
if ($inherited) {
if (!array_key_exists('inherited', $mapping)) {
Expand Down Expand Up @@ -916,10 +916,7 @@ protected function validateAndCompleteFieldMapping(array $mapping, self $inherit
return $mapping;
}

/**
* @param string|bool $phpcrLabel
*/
protected function validateAndCompleteAssociationMapping(array $mapping, self $inherited = null, $phpcrLabel = 'property'): array
private function validateAndCompleteAssociationMapping(array $mapping, self $inherited = null, bool|string $phpcrLabel = 'property'): array
{
$mapping = $this->validateAndCompleteFieldMapping($mapping, $inherited, false, $phpcrLabel);
if ($inherited) {
Expand Down Expand Up @@ -1083,7 +1080,7 @@ public function mapManyToMany(array $mapping, self $inherited = null): void
/**
* Sets the ID generator used to generate IDs for instances of this class.
*/
protected function setIdGenerator(int|string $generator): void
private function setIdGenerator(int|string $generator): void
{
if (is_string($generator)) {
$generator = constant('Doctrine\ODM\PHPCR\Mapping\ClassMetadata::GENERATOR_TYPE_'.strtoupper($generator));
Expand Down
16 changes: 7 additions & 9 deletions lib/Doctrine/ODM/PHPCR/Query/Builder/AbstractLeafNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
*/
abstract class AbstractLeafNode extends AbstractNode
{
public function getNext()
public function getNext(): ?AbstractNode
{
return $this->getParent();
}

public function getChildren()
public function getChildren(): array
{
throw new RuntimeException(sprintf(
'Cannot call getChildren on leaf node "%s"',
$this->getName()
));
}

public function addChild(AbstractNode $node)
public function addChild(AbstractNode $node): static
{
throw new RuntimeException(sprintf(
'Cannot call addChild to leaf node "%s"',
$this->getName()
));
}

public function getCardinalityMap()
public function getCardinalityMap(): array
{
// no children , no cardinality map...
return [];
Expand All @@ -47,15 +47,13 @@ public function getCardinalityMap()
*
* e.g. my_alias.first_name
*
* @param string $field
*
* @return array
* @return string[] with exactly 2 elements
*/
protected function explodeField($field)
protected function explodeField(string $field): array
{
$parts = explode('.', $field);

if (2 == count($parts)) {
if (2 === count($parts)) {
return $parts;
}

Expand Down
90 changes: 32 additions & 58 deletions lib/Doctrine/ODM/PHPCR/Query/Builder/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ abstract class AbstractNode

public const NT_WHERE_OR = 'where_or';

protected $children = [];
private array $children = [];

protected $parent;
private ?AbstractNode $parent;

public function __construct(self $parent = null)
public function __construct(AbstractNode $parent = null)
{
$this->parent = $parent;
}
Expand All @@ -73,17 +73,13 @@ public function __construct(self $parent = null)
* Return the type of node.
*
* Must be one of self::NT_*
*
* @return string
*/
abstract public function getNodeType();
abstract public function getNodeType(): string;

/**
* Return the parent of this node.
*
* @return AbstractNode
*/
public function getParent()
public function getParent(): ?AbstractNode
{
return $this->parent;
}
Expand All @@ -93,15 +89,12 @@ public function getParent()
*
* <strike>This should only be used when generating exceptions</strike>
* This is also used to determine the dispatching method -- should it be?
*
* @return string
*/
final public function getName()
final public function getName(): string
{
$refl = new \ReflectionClass($this);
$short = $refl->getShortName();

return $short;
return $refl->getShortName();
}

/**
Expand All @@ -123,20 +116,18 @@ final public function getName()
* self::NT_PROPERTY => array(null, 1), // require none to 1 properties
* );
*
* @return array
* @return array<string, array<int|null>>
*/
abstract public function getCardinalityMap();
abstract public function getCardinalityMap(): array;

/**
* Remove any previous children and add
* given node via. addChild.
*
* @see removeChildrenOfType
* @see addChild
*
* @return AbstractNode
*/
public function setChild(self $node)
public function setChild(AbstractNode $node): AbstractNode
{
$this->removeChildrenOfType($node->getNodeType());

Expand All @@ -154,11 +145,9 @@ public function setChild(self $node)
* The given node will be returned EXCEPT when the current
* node is a leaf node, in which case we return the parent.
*
* @return AbstractNode
*
* @throws OutOfBoundsException
*/
public function addChild(self $node)
public function addChild(AbstractNode $node): AbstractNode
{
$cardinalityMap = $this->getCardinalityMap();
$nodeType = $node->getNodeType();
Expand Down Expand Up @@ -199,12 +188,10 @@ public function addChild(self $node)

/**
* Return the next object in the fluid interface
* chain. Leaf nodes always return the parent, deafult
* chain. Leaf nodes always return the parent, default
* behavior is to return /this/ class.
*
* @return AbstractNode
*/
public function getNext()
public function getNext(): ?AbstractNode
{
return $this;
}
Expand All @@ -217,7 +204,7 @@ public function getNext()
*
* @return AbstractNode[]
*/
public function getChildren()
public function getChildren(): array
{
$children = [];
foreach ($this->children as $type) {
Expand All @@ -232,36 +219,29 @@ public function getChildren()
/**
* Return children of specified type.
*
* @param string $type the type name
*
* @return AbstractNode[]
*/
public function getChildrenOfType($type)
public function getChildrenOfType(string $type): array
{
return $this->children[$type] ?? [];
}

public function removeChildrenOfType($type)
public function removeChildrenOfType(string $type): void
{
unset($this->children[$type]);
}

/**
* Return child of node, there must be exactly one child of any type.
*
* @return AbstractNode
*
* @throws OutOfBoundsException if there are more than one or none
*/
public function getChild()
public function getChild(): AbstractNode
{
$children = $this->getChildren();

if (!$children) {
throw new OutOfBoundsException(sprintf(
'Expected exactly one child, got "%s"',
count($children)
));
throw new OutOfBoundsException('Expected exactly one child, got none');
}

if (count($children) > 1) {
Expand All @@ -279,21 +259,16 @@ public function getChild()
*
* Note: This does not take inheritance into account.
*
* @param string $type the name of the type
*
* @return AbstractNode
*
* @throws OutOfBoundsException if there are more than one or none
*/
public function getChildOfType($type)
public function getChildOfType(string $type): AbstractNode
{
$children = $this->getChildrenOfType($type);

if (!$children) {
throw new OutOfBoundsException(sprintf(
'Expected exactly one child of type "%s", got "%s"',
'Expected exactly one child of type "%s", got none',
$type,
count($children)
));
}

Expand All @@ -313,13 +288,13 @@ public function getChildOfType($type)
* Validation is performed both when the query is being
* built and when the dev explicitly calls "end()".
*
* This method simply checks the minimum boundries are satisfied,
* the addChild() method already validates maximum boundries and
* This method simply checks the minimum boundaries are satisfied,
* the addChild() method already validates maximum boundaries and
* types.
*
* @throws OutOfBoundsException
*/
public function validate()
public function validate(): void
{
$cardinalityMap = $this->getCardinalityMap();
$typeCount = [];
Expand Down Expand Up @@ -351,10 +326,8 @@ public function validate()
* Validates this node and returns its parent.
* This should be used if the developer wants to
* define the entire Query in a fluid manner.
*
* @return AbstractNode
*/
public function end()
public function end(): ?AbstractNode
{
$this->validate();

Expand All @@ -370,7 +343,7 @@ public function end()
*
* @throws BadMethodCallException if an unknown method is called
*/
public function __call($methodName, $args)
public function __call(string $methodName, array $args): never
{
throw new BadMethodCallException(sprintf(
'Unknown method "%s" called on node "%s", did you mean one of: "%s"',
Expand All @@ -380,7 +353,7 @@ public function __call($methodName, $args)
));
}

public function ensureNoArguments($method, $void)
public function ensureNoArguments(string $method, $void): void
{
if ($void) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -390,17 +363,18 @@ public function ensureNoArguments($method, $void)
}
}

public function getFactoryMethods()
/**
* @return string[]
*/
public function getFactoryMethods(): array
{
$refl = new \ReflectionClass($this);

$fMethods = [];
foreach ($refl->getMethods() as $rMethod) {
$comment = $rMethod->getDocComment();
if ($comment) {
if (false !== strpos($comment, '@factoryMethod')) {
$fMethods[] = $rMethod->name;
}
if ($comment && str_contains($comment, '@factoryMethod')) {
$fMethods[] = $rMethod->name;
}
}

Expand Down

0 comments on commit a5c4988

Please sign in to comment.