Skip to content

Commit

Permalink
Merge pull request #74 from mapado/jd-feat-prettierPhp
Browse files Browse the repository at this point in the history
try prettier php
  • Loading branch information
jdeniau authored Apr 10, 2018
2 parents 6855959 + c4f3e6d commit 680e765
Show file tree
Hide file tree
Showing 14 changed files with 447 additions and 192 deletions.
8 changes: 8 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"src/**/*.php":
- "yarn run prettier --write"
- "vendor/bin/php-cs-fixer fix --config=.php_cs"
- "git add"

"Tests/**/*.php":
- "vendor/bin/php-cs-fixer fix --config=.php_cs"
- "git add"
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"dependencies": {
"@prettier/plugin-php": "^0.1.0",
"husky": "^0.14.3",
"lint-staged": "^7.0.4"
"lint-staged": "^7.0.4",
"prettier": "^1.11.1"
},
"scripts": {
"precommit": "lint-staged"
},
"lint-staged": {
"*.php": [
"vendor/bin/php-cs-fixer fix --config=.php_cs",
"git add"
]
}
}
10 changes: 7 additions & 3 deletions src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class Collection implements \IteratorAggregate, \Serializable, \Countable, \Arra
* @param array $elements the data elements as an array
* @param array $extraProperties the extra properties
*/
public function __construct(array $elements = [], array $extraProperties = [])
{
public function __construct(
array $elements = [],
array $extraProperties = []
) {
$this->elements = $elements;
$this->extraProperties = $extraProperties;
}
Expand Down Expand Up @@ -115,7 +117,9 @@ public function offsetUnset($offset)
*/
public function offsetGet($offset)
{
return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
return isset($this->elements[$offset])
? $this->elements[$offset]
: null;
}

/**
Expand Down
106 changes: 69 additions & 37 deletions src/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ class EntityRepository
* @param RestClient $restClient The client to process the http requests
* @param string $entityName The entity to work with
*/
public function __construct(SdkClient $sdkClient, RestClient $restClient, UnitOfWork $unitOfWork, $entityName)
{
public function __construct(
SdkClient $sdkClient,
RestClient $restClient,
UnitOfWork $unitOfWork,
$entityName
) {
$this->sdk = $sdkClient;
$this->restClient = $restClient;
$this->unitOfWork = $unitOfWork;
Expand Down Expand Up @@ -78,13 +82,17 @@ public function __call($method, $arguments)

default:
throw new \BadMethodCallException(
'Undefined method \'' . $method . '\'. The method name must start with
'Undefined method \'' .
$method .
'\'. The method name must start with
either findBy or findOneBy!'
);
}

if (empty($arguments)) {
throw new SdkException('You need to pass a parameter to ' . $method);
throw new SdkException(
'You need to pass a parameter to ' . $method
);
}

$mapping = $this->sdk->getMapping();
Expand All @@ -97,7 +105,8 @@ public function __call($method, $arguments)
} else {
$queryParams = current($arguments);
}
$path .= '?' . http_build_query($this->convertQueryParameters($queryParams));
$path .=
'?' . http_build_query($this->convertQueryParameters($queryParams));

// if entityList is found in cache, return it
$entityListFromCache = $this->fetchFromCache($path);
Expand Down Expand Up @@ -230,16 +239,31 @@ public function remove($model)
*
* @return object
*/
public function update($model, $serializationContext = [], $queryParams = [])
{
public function update(
$model,
$serializationContext = [],
$queryParams = []
) {
$identifier = $model->{$this->getClassMetadata()->getIdGetter()}();
$serializer = $this->sdk->getSerializer();
$newSerializedModel = $serializer->serialize($model, $this->entityName, $serializationContext);
$newSerializedModel = $serializer->serialize(
$model,
$this->entityName,
$serializationContext
);

$oldModel = $this->unitOfWork->getDirtyEntity($identifier);
if ($oldModel) {
$oldSerializedModel = $serializer->serialize($oldModel, $this->entityName, $serializationContext);
$newSerializedModel = $this->unitOfWork->getDirtyData($newSerializedModel, $oldSerializedModel, $this->getClassMetadata());
$oldSerializedModel = $serializer->serialize(
$oldModel,
$this->entityName,
$serializationContext
);
$newSerializedModel = $this->unitOfWork->getDirtyData(
$newSerializedModel,
$oldSerializedModel,
$this->getClassMetadata()
);
}

$data = $this->restClient->put(
Expand All @@ -262,20 +286,29 @@ public function update($model, $serializationContext = [], $queryParams = [])
*
* @return object
*/
public function persist($model, $serializationContext = [], $queryParams = [])
{
public function persist(
$model,
$serializationContext = [],
$queryParams = []
) {
$mapping = $this->sdk->getMapping();
$prefix = $mapping->getIdPrefix();
$key = $mapping->getKeyFromModel($this->entityName);

$path = empty($prefix) ? '/' . $key : $prefix . '/' . $key;

$oldSerializedModel = $this->getClassMetadata()->getDefaultSerializedModel();
$newSerializedModel = $this->sdk->getSerializer()
->serialize($model, $this->entityName, $serializationContext);
$newSerializedModel = $this->sdk->getSerializer()->serialize(
$model,
$this->entityName,
$serializationContext
);

$diff = $this->unitOfWork
->getDirtyData($newSerializedModel, $oldSerializedModel, $this->getClassMetadata());
$diff = $this->unitOfWork->getDirtyData(
$newSerializedModel,
$oldSerializedModel,
$this->getClassMetadata()
);

$data = $this->restClient->post(
$this->addQueryParameter($path, $queryParams),
Expand Down Expand Up @@ -381,31 +414,30 @@ private function convertQueryParameters($queryParameters)
{
$mapping = $this->sdk->getMapping();

return array_map(
function ($item) use ($mapping) {
if (is_object($item)) {
$classname = get_class($item);
return array_map(function ($item) use ($mapping) {
if (is_object($item)) {
$classname = get_class($item);

if ($mapping->hasClassMetadata($classname)) {
$idAttr = $mapping->getClassMetadata($classname)
->getIdentifierAttribute();
if ($mapping->hasClassMetadata($classname)) {
$idAttr = $mapping->getClassMetadata(
$classname
)->getIdentifierAttribute();

if ($idAttr) {
$idGetter = 'get' . ucfirst($idAttr->getAttributeName());
if ($idAttr) {
$idGetter =
'get' . ucfirst($idAttr->getAttributeName());

return $item->{$idGetter}();
}
return $item->{$idGetter}();
}
}

if (method_exists($item, 'getId')) {
return call_user_func([$item, 'getId']);
}
if (method_exists($item, 'getId')) {
return call_user_func([$item, 'getId']);
}
}

return $item;
},
$queryParameters
);
return $item;
}, $queryParameters);
}

/**
Expand All @@ -421,9 +453,9 @@ private function normalizeCacheKey($key)
private function getClassMetadata()
{
if (!isset($this->classMetadata)) {
$this->classMetadataCache = $this->sdk
->getMapping()
->getClassMetadata($this->entityName);
$this->classMetadataCache = $this->sdk->getMapping()->getClassMetadata(
$this->entityName
);
}

return $this->classMetadataCache;
Expand Down
10 changes: 8 additions & 2 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ public static function arrayDot($array, $prepend = '')
$results = [];
foreach ($array as $key => $value) {
if (is_array($value) && !empty($value)) {
$results = array_merge($results, static::arrayDot($value, $prepend . $key . '.'));
$results = array_merge(
$results,
static::arrayDot($value, $prepend . $key . '.')
);
} else {
$results[$prepend . $key] = $value;
}
Expand All @@ -92,7 +95,10 @@ public static function arrayDot($array, $prepend = '')

public static function arrayDiffAssocRecursive($array1, $array2)
{
return array_diff_assoc(static::arrayDot($array1), static::arrayDot($array2));
return array_diff_assoc(
static::arrayDot($array1),
static::arrayDot($array2)
);
}

public static function arraySame($array1, $array2)
Expand Down
33 changes: 16 additions & 17 deletions src/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
*/
class Mapping
{
public const DEFAULT_CONFIG = [
'collectionKey' => 'hydra:member',
];
public const DEFAULT_CONFIG = ['collectionKey' => 'hydra:member'];

/**
* @var string
Expand Down Expand Up @@ -79,10 +77,7 @@ public function getConfig()
*/
public function setConfig(array $config)
{
$this->config = array_merge(
self::DEFAULT_CONFIG,
$config
);
$this->config = array_merge(self::DEFAULT_CONFIG, $config);

return $this;
}
Expand Down Expand Up @@ -122,12 +117,9 @@ public function getModelName($key)
*/
public function getMappingKeys()
{
return array_map(
function (ClassMetadata $classMetadata) {
return $classMetadata->getKey();
},
$this->classMetadataList
);
return array_map(function (ClassMetadata $classMetadata) {
return $classMetadata->getKey();
}, $this->classMetadataList);
}

/**
Expand Down Expand Up @@ -162,7 +154,9 @@ public function getKeyFromModel($modelName)
}
}

throw new MappingException('Model name ' . $modelName . ' not found in mapping');
throw new MappingException(
'Model name ' . $modelName . ' not found in mapping'
);
}

/**
Expand Down Expand Up @@ -273,8 +267,10 @@ private function checkMappingExistence($key, $subKey = null)

if (!empty($subKey)) {
$methodName = 'get' . ucfirst($subKey);
if (!$metadata->$methodName()) {
throw new MappingException($key . ' key is mapped but no ' . $subKey . ' found');
if (!$metadata->{$methodName}()) {
throw new MappingException(
$key . ' key is mapped but no ' . $subKey . ' found'
);
}
}
}
Expand All @@ -288,7 +284,10 @@ private function checkMappingExistence($key, $subKey = null)
*/
private function removePrefix($value)
{
if (($this->idPrefixLength > 0) && (0 === strpos($value, $this->idPrefix))) {
if (
($this->idPrefixLength > 0) &&
(0 === strpos($value, $this->idPrefix))
) {
return substr($value, $this->idPrefixLength);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public function setKey($key)
*/
public function getAttribute($name)
{
return isset($this->attributeList[$name]) ? $this->attributeList[$name] : null;
return isset($this->attributeList[$name])
? $this->attributeList[$name]
: null;
}

/**
Expand Down Expand Up @@ -240,8 +242,7 @@ public function getIdGetter()
public function getIdSerializeKey()
{
if ($this->getIdentifierAttribute()) {
$idAttr = $this->getIdentifierAttribute()
->getSerializedKey();
$idAttr = $this->getIdentifierAttribute()->getSerializedKey();

return $idAttr;
} else {
Expand Down Expand Up @@ -279,8 +280,7 @@ public function getDefaultSerializedModel()
private function getIdKey()
{
if ($this->getIdentifierAttribute()) {
$idAttr = $this->getIdentifierAttribute()
->getAttributeName();
$idAttr = $this->getIdentifierAttribute()->getAttributeName();

return $idAttr;
} else {
Expand Down
Loading

0 comments on commit 680e765

Please sign in to comment.