Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Upgrade code syntax to PHP 7 & CS
Browse files Browse the repository at this point in the history
- Add typehints
- Remove SF2.3 code
- Update Coding-Style to SF guidelines
  • Loading branch information
Maks3w committed May 15, 2019
1 parent 4f970c7 commit f01aa0f
Show file tree
Hide file tree
Showing 34 changed files with 184 additions and 366 deletions.
23 changes: 0 additions & 23 deletions .php_cs

This file was deleted.

12 changes: 12 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
])
->setFinder($finder)
;
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ matrix:
before_install:
- phpenv config-add .ci/php.ini || return 0
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- if [[ $EXECUTE_CS_CHECK != 'true' ]]; then travis_retry composer remove --dev --no-update fabpot/php-cs-fixer ; fi
- if [[ $EXECUTE_CS_CHECK != 'true' ]]; then travis_retry composer remove --dev --no-update friendsofphp/php-cs-fixer ; fi

install:
- travis_retry composer update --no-interaction $COMPOSER_ARGUMENTS
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
Expand Down Expand Up @@ -67,7 +64,7 @@ public function getConfigTreeBuilder()
->end()
->end()
->validate()
->ifTrue(function ($v) {
->ifTrue(static function ($v) {
return $v['driver']['useSsl'] && $v['driver']['useStartTls'];
})
->thenInvalid('The useSsl and useStartTls options are mutually exclusive.')
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/FR3DLdapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

class FR3DLdapExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
foreach (['services', 'security', 'validator', 'ldap_driver'] as $basename) {
$loader->load(sprintf('%s.xml', $basename));
}
Expand Down
30 changes: 15 additions & 15 deletions Driver/LdapDriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ interface LdapDriverInterface
/**
* Bind to LDAP directory.
*
* @param UserInterface $user The user for authenticating the bind.
* @param string $password The password for authenticating the bind.
* @param UserInterface $user the user for authenticating the bind
* @param string $password the password for authenticating the bind
*
* @return bool true on success or false on failure
*
* @throws LdapDriverException if some error occurs.
* @throws LdapDriverException if some error occurs
*/
public function bind(UserInterface $user, $password);
public function bind(UserInterface $user, string $password): bool;

/**
* Search LDAP tree.
*
* @param string $baseDn The base DN for the directory.
* @param string $filter The search filter.
* @param array $attributes The array of the required attributes,
* 'dn' is always returned. If array is
* empty then will return all attributes
* and their associated values.
* @param string $baseDn the base DN for the directory
* @param string $filter the search filter
* @param array $attributes The array of the required attributes,
* 'dn' is always returned. If array is
* empty then will return all attributes
* and their associated values.
*
* @return array|bool Returns a complete result information in a
* multi-dimensional array on success and FALSE on error.
* see {@link http://www.php.net/function.ldap-get-entries.php}
* for array format examples.
* multi-dimensional array on success and FALSE on error.
* see {@link http://www.php.net/function.ldap-get-entries.php}
* for array format examples.
*
* @throws LdapDriverException if some error occurs.
* @throws LdapDriverException if some error occurs
*/
public function search($baseDn, $filter, array $attributes = []);
public function search(string $baseDn, string $filter, array $attributes = []);
}
24 changes: 6 additions & 18 deletions Driver/ZendLdapDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ class ZendLdapDriver implements LdapDriverInterface

/**
* @param Ldap $driver Initialized Zend::Ldap Object
* @param LoggerInterface $logger Optional logger for write debug messages.
* @param LoggerInterface $logger optional logger for write debug messages
*/
public function __construct(Ldap $driver, LoggerInterface $logger = null)
{
$this->driver = $driver;
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
public function search($baseDn, $filter, array $attributes = [])
public function search(string $baseDn, string $filter, array $attributes = [])
{
$this->logDebug('{action}({base_dn}, {filter}, {attributes})', [
'action' => 'ldap_search',
Expand All @@ -64,10 +61,7 @@ public function search($baseDn, $filter, array $attributes = [])
return $entries;
}

/**
* {@inheritdoc}
*/
public function bind(UserInterface $user, $password)
public function bind(UserInterface $user, string $password): bool
{
if ($user instanceof LdapUserInterface && $user->getDn()) {
$bind_rdn = $user->getDn();
Expand All @@ -82,7 +76,7 @@ public function bind(UserInterface $user, $password)
]);
$bind = $this->driver->bind($bind_rdn, $password);

return ($bind instanceof Ldap);
return $bind instanceof Ldap;
} catch (ZendLdapException $exception) {
$this->zendExceptionHandler($exception, $password);
}
Expand All @@ -92,11 +86,8 @@ public function bind(UserInterface $user, $password)

/**
* Treat a Zend Ldap Exception.
*
* @param ZendLdapException $exception
* @param string $password
*/
protected function zendExceptionHandler(ZendLdapException $exception, $password = null)
protected function zendExceptionHandler(ZendLdapException $exception, string $password = null): void
{
$sanitizedException = null !== $password ? new SanitizingException($exception, $password) : $exception;

Expand All @@ -117,11 +108,8 @@ protected function zendExceptionHandler(ZendLdapException $exception, $password

/**
* Log debug messages if the logger is set.
*
* @param string $message
* @param array $context
*/
private function logDebug($message, array $context = [])
private function logDebug(string $message, array $context = []): void
{
if ($this->logger) {
$this->logger->debug($message, $context);
Expand Down
9 changes: 2 additions & 7 deletions Hydrator/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FR3D\LdapBundle\Hydrator;

use FR3D\LdapBundle\Model\LdapUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Provide a hydrator template for easy implementation.
Expand All @@ -21,10 +22,7 @@ public function __construct(array $attributeMap)
$this->attributeMap = $attributeMap['attributes'];
}

/**
* {@inheritdoc}
*/
public function hydrate(array $ldapEntry)
public function hydrate(array $ldapEntry): UserInterface
{
$user = $this->createUser();

Expand All @@ -37,8 +35,5 @@ public function hydrate(array $ldapEntry)
return $user;
}

/**
* {@inheritdoc}
*/
abstract protected function createUser();
}
12 changes: 6 additions & 6 deletions Hydrator/HydrateWithMapTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ trait HydrateWithMapTrait
/**
* Fill the given user with the following the attribute-method map.
*
* @param UserInterface $user Target user.
* @param array[] $ldapUserAttributes Raw LDAP data.
* @param string[] $attributeMap Attribute-method map.
* @param UserInterface $user target user
* @param array[] $ldapUserAttributes raw LDAP data
* @param string[] $attributeMap attribute-method map
*/
protected function hydrateUserWithAttributesMap(
UserInterface $user,
array $ldapUserAttributes,
array $attributeMap
) {
): void {
foreach ($attributeMap as $attr) {
if (!array_key_exists($attr['ldap_attr'], $ldapUserAttributes)) {
continue;
Expand All @@ -32,13 +32,13 @@ protected function hydrateUserWithAttributesMap(
unset($ldapValue['count']);
}

if (count($ldapValue) === 1) {
if (1 === count($ldapValue)) {
$value = array_shift($ldapValue);
} else {
$value = $ldapValue;
}

call_user_func([$user, $attr['user_method']], $value);
$user->{$attr['user_method']}($value);
}
}
}
6 changes: 2 additions & 4 deletions Hydrator/HydratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ interface HydratorInterface
* Populate an user with the data retrieved from LDAP.
*
* @param array $ldapEntry LDAP result information as a multi-dimensional array.
* see {@link http://www.php.net/function.ldap-get-entries.php} for array format examples.
*
* @return UserInterface
* see {@link http://www.php.net/function.ldap-get-entries.php} for array format examples.
*/
public function hydrate(array $ldapEntry);
public function hydrate(array $ldapEntry): UserInterface;
}
9 changes: 1 addition & 8 deletions Hydrator/LegacyHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@ final class LegacyHydrator extends AbstractHydrator
*/
private $userManager;

/**
* @param UserManagerInterface $userManager
* @param array $attributeMap
*/
public function __construct($userManager, array $attributeMap)
public function __construct(UserManagerInterface $userManager, array $attributeMap)
{
parent::__construct($attributeMap);

$this->userManager = $userManager;
}

/**
* {@inheritdoc}
*/
protected function createUser()
{
$user = $this->userManager->createUser();
Expand Down
24 changes: 5 additions & 19 deletions Ldap/LdapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,20 @@ public function __construct(LdapDriverInterface $driver, HydratorInterface $hydr
$this->hydrator = $hydrator;
}

/**
* {@inheritdoc}
*/
public function findUserByUsername($username)
public function findUserByUsername(string $username): ?UserInterface
{
return $this->findUserBy([$this->params['usernameAttribute'] => $username]);
}

/**
* {@inheritdoc}
*/
public function findUserBy(array $criteria)
public function findUserBy(array $criteria): ?UserInterface
{
$filter = $this->buildFilter($criteria);
$entries = $this->driver->search($this->params['baseDn'], $filter);
if ($entries['count'] > 1) {
throw new \Exception('This search can only return a single user');
}

if ($entries['count'] == 0) {
if (0 === $entries['count']) {
return null;
}
$user = $this->hydrator->hydrate($entries[0]);
Expand All @@ -52,13 +46,8 @@ public function findUserBy(array $criteria)

/**
* Build Ldap filter.
*
* @param array $criteria
* @param string $condition
*
* @return string
*/
protected function buildFilter(array $criteria, $condition = '&')
protected function buildFilter(array $criteria, string $condition = '&'): string
{
$filters = [];
$filters[] = $this->params['filter'];
Expand All @@ -70,10 +59,7 @@ protected function buildFilter(array $criteria, $condition = '&')
return sprintf('(%s%s)', $condition, implode($filters));
}

/**
* {@inheritdoc}
*/
public function bind(UserInterface $user, $password)
public function bind(UserInterface $user, string $password): bool
{
return $this->driver->bind($user, $password);
}
Expand Down
15 changes: 3 additions & 12 deletions Ldap/LdapManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,19 @@ interface LdapManagerInterface
/**
* Find a user by its username.
*
* @param string $username
*
* @return UserInterface|null The user or null if the user does not exist
*/
public function findUserByUsername($username);
public function findUserByUsername(string $username): ?UserInterface;

/**
* Finds one user by the given criteria.
*
* @param array $criteria
*
* @return UserInterface|null The user or null if the user does not exist
*/
public function findUserBy(array $criteria);
public function findUserBy(array $criteria): ?UserInterface;

/**
* Bind the user on ldap.
*
* @param UserInterface $user
* @param string $password
*
* @return bool
*/
public function bind(UserInterface $user, $password);
public function bind(UserInterface $user, string $password): bool;
}
Loading

0 comments on commit f01aa0f

Please sign in to comment.