Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Psalm #74

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ php:
- '7.4'
install:
- composer install
jobs:
include:
- name: Psalm
script: vendor/bin/psalm
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"symfony/var-dumper": "^3.3",
"symfony/debug": "^3.3",
"mark-gerarts/phpstan-automapper-plus": "^0.1.0",
"phpunit/phpunit": "^7.0"
"phpunit/phpunit": "^7.0",
"vimeo/psalm": "^4.6"
}
}
26 changes: 26 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.6.2@bca09d74adc704c4eaee36a3c3e9d379e290fc3b">
<file src="src/AutoMapper.php">
<TooManyArguments occurrences="1">
<code>mapToObject</code>
</TooManyArguments>
</file>
<file src="src/MappingOperation/Implementations/MapTo.php">
<TooManyArguments occurrences="2">
<code>map</code>
<code>mapMultiple</code>
</TooManyArguments>
</file>
<file src="src/PropertyAccessor/ArrayPropertyReader.php">
<LessSpecificImplementedReturnType occurrences="1">
<code>array</code>
</LessSpecificImplementedReturnType>
<MoreSpecificImplementedParamType occurrences="1">
<code>$array</code>
</MoreSpecificImplementedParamType>
<ParamNameMismatch occurrences="2">
<code>$array</code>
<code>$array</code>
</ParamNameMismatch>
</file>
</files>
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<psalm
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
20 changes: 10 additions & 10 deletions src/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function initialize(callable $configurator): AutoMapperInterface
return $mapper;
}

private function push($key, $value, &$context)
private function push($key, $value, &$context): void
{
if (!array_key_exists($key, $context)) {
$stack = [];
Expand All @@ -62,15 +62,15 @@ private function push($key, $value, &$context)
$context[$key] = $stack;
}

private function pop($key, &$context)
private function pop($key, &$context): void
{
array_pop($context[$key]);
}

/**
* @inheritdoc
*/
public function map($source, string $destinationClass, array $context = [])
public function map($source, string $targetClass, array $context = [])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those renames are probably due to PHP 8 named arguments.

{
if ($source === null) {
return null;
Expand All @@ -85,11 +85,11 @@ public function map($source, string $destinationClass, array $context = [])
}
}

$context[self::DESTINATION_CLASS_CONTEXT] = $destinationClass;
$context[self::DESTINATION_CLASS_CONTEXT] = $targetClass;

$mapping = $this->getMapping($sourceClass, $destinationClass);
$mapping = $this->getMapping($sourceClass, $targetClass);
if ($mapping->providesCustomMapper()) {
return $this->getCustomMapper($mapping)->map($source, $destinationClass);
return $this->getCustomMapper($mapping)->map($source, $targetClass);
}

if ($mapping->hasCustomConstructor()) {
Expand All @@ -98,14 +98,14 @@ public function map($source, string $destinationClass, array $context = [])
$this,
$context
);
} elseif (interface_exists($destinationClass)) {
} elseif (interface_exists($targetClass)) {
// If we're mapping to an interface a valid custom constructor has
// to be provided. Otherwise we can't know what to do.
$message = 'Mapping to an interface is not possible. Please '
. 'provide a concrete class or use mapToObject instead.';
throw new AutoMapperPlusException($message);
} else {
$destinationObject = new $destinationClass;
$destinationObject = new $targetClass;
}

$context[self::DESTINATION_CONTEXT] = $destinationObject;
Expand All @@ -126,7 +126,7 @@ public function map($source, string $destinationClass, array $context = [])
*/
public function mapMultiple(
$sourceCollection,
string $destinationClass,
string $targetClass,
array $context = []
): array
{
Expand All @@ -138,7 +138,7 @@ public function mapMultiple(

$mappedResults = [];
foreach ($sourceCollection as $source) {
$mappedResults[] = $this->map($source, $destinationClass, $context);
$mappedResults[] = $this->map($source, $targetClass, $context);
}

return $mappedResults;
Expand Down
5 changes: 3 additions & 2 deletions src/Configuration/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class Options
private $useSubstitution = true;

/**
* @var string[]
* @var bool[]
* @psalm-var array<class-string,bool>
*/
private $objectCrates = [];

Expand Down Expand Up @@ -280,7 +281,7 @@ public function providesCustomMapper(): bool
}

/**
* @param string $className
* @param class-string $className
*/
public function registerObjectCrate(string $className): void
{
Expand Down
6 changes: 6 additions & 0 deletions src/MapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ interface MapperInterface
* Maps an object to an instance of class $to, provided a mapping is
* configured.
*
* @template T of object
Copy link
Author

@Jean85 Jean85 Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These templates are the main improvements for the end user.

* @param array|object $source
* The source object.
* @param string $targetClass
* The target classname.
* @psalm-param class-string<T> $targetClass
* @param array $context
* An arbitrary array of values that will be passed to supporting
* mapping operations (e.g. MapFrom) to alter their behaviour based on
* the context.
* This is not explicitly required on the interface yet to preserve
* backwards compatibility, but will be added in version 2.0.
* @return mixed
* @psalm-return T
* An instance of class $to.
* @throws UnregisteredMappingException
*/
Expand All @@ -34,13 +37,16 @@ public function map($source, string $targetClass/**, array $context = [] */);
/**
* Maps properties of object $from to an existing object $to.
*
* @template T of object
* @param array|object $source
* The source object.
* @param object $destination
* The target object.
* @psalm-param T $destination
* @param array $context
* See MapperInterface::map()
* @return mixed
* @psalm-return T
* $to, with properties copied from $from.
* @throws UnregisteredMappingException
*/
Expand Down
1 change: 1 addition & 0 deletions src/MappingOperation/DefaultMappingOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AutoMapperPlus\Configuration\Options;
use AutoMapperPlus\NameResolver\NameResolverInterface;
use AutoMapperPlus\PropertyAccessor\PropertyAccessorInterface;
use AutoMapperPlus\PropertyAccessor\PropertyReaderInterface;
use AutoMapperPlus\PropertyAccessor\PropertyWriterInterface;

Expand Down
6 changes: 3 additions & 3 deletions src/PropertyAccessor/PropertyReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
interface PropertyReaderInterface
{
/**
* @param $object
* @param object|array $object
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to be more correct, but the ArrayPropertyReader breaks the Liskov principle... What can we do?

* @param string $propertyName
* @return bool
*/
public function hasProperty($object, string $propertyName): bool;

/**
* @param $object
* @param object|array $object
* @param string $propertyName
* @return mixed
*/
Expand All @@ -26,7 +26,7 @@ public function getProperty($object, string $propertyName);
/**
* Returns a list of property names available on the object.
*
* @param object $object
* @param object|array $object
* @return string[]
*/
public function getPropertyNames($object): array;
Expand Down