@@ -65,12 +65,12 @@ class Ldap
6565 * @psalm-suppress NullArgument
6666 */
6767 public function __construct (
68- $ hostname ,
69- $ enable_tls = true ,
70- $ debug = false ,
71- $ timeout = 0 ,
72- $ port = 389 ,
73- $ referrals = true
68+ string $ hostname ,
69+ bool $ enable_tls = true ,
70+ bool $ debug = false ,
71+ int $ timeout = 0 ,
72+ int $ port = 389 ,
73+ bool $ referrals = true
7474 ) {
7575 // Debug
7676 Logger::debug ('Library - LDAP __construct(): Setup LDAP with ' .
@@ -152,7 +152,7 @@ public function __construct(
152152 * @param int|null $type The exception's type
153153 * @return \Exception
154154 */
155- private function makeException ($ description , $ type = null )
155+ private function makeException (string $ description , int $ type = null ): \ Exception
156156 {
157157 $ errNo = @ldap_errno ($ this ->ldap );
158158
@@ -234,8 +234,13 @@ private function makeException($description, $type = null)
234234 * - Zero entries were found
235235 * @psalm-suppress TypeDoesNotContainType
236236 */
237- private function search ($ base , $ attribute , $ value , $ searchFilter = null , $ scope = "subtree " )
238- {
237+ private function search (
238+ string $ base ,
239+ $ attribute ,
240+ string $ value ,
241+ ?string $ searchFilter = null ,
242+ string $ scope = "subtree "
243+ ): string {
239244 // Create the search filter
240245 /** @var array $attribute */
241246 $ attribute = self ::escapeFilterValue ($ attribute , false );
@@ -342,11 +347,11 @@ private function search($base, $attribute, $value, $searchFilter = null, $scope
342347 public function searchfordn (
343348 $ base ,
344349 $ attribute ,
345- $ value ,
346- $ allowZeroHits = false ,
347- $ searchFilter = null ,
348- $ scope = 'subtree '
349- ) {
350+ string $ value ,
351+ bool $ allowZeroHits = false ,
352+ ? string $ searchFilter = null ,
353+ string $ scope = 'subtree '
354+ ): ? string {
350355 // Traverse all search bases, returning DN if found
351356 $ bases = Utils \Arrays::arrayize ($ base );
352357 foreach ($ bases as $ current ) {
@@ -395,10 +400,10 @@ public function searchformultiple(
395400 $ bases ,
396401 $ filters ,
397402 $ attributes = [],
398- $ and = true ,
399- $ escape = true ,
400- $ scope = 'subtree '
401- ) {
403+ bool $ and = true ,
404+ bool $ escape = true ,
405+ string $ scope = 'subtree '
406+ ): array {
402407 // Escape the filter values, if requested
403408 if ($ escape ) {
404409 $ filters = $ this ->escapeFilterValue ($ filters , false );
@@ -414,6 +419,7 @@ public function searchformultiple(
414419 $ filter = ($ and ? '(& ' : '(| ' ) . $ filter . ') ' ;
415420 }
416421 } else {
422+ /** @psalm-suppress RedundantConditionGivenDocblockType */
417423 Assert::string ($ filters );
418424 $ filter = $ filters ;
419425 }
@@ -516,7 +522,7 @@ public function searchformultiple(
516522 * LDAP_INAPPROPRIATE_AUTH, LDAP_INSUFFICIENT_ACCESS
517523 * @throws Error\Exception on other errors
518524 */
519- public function bind ($ dn , $ password , array $ sasl_args = null )
525+ public function bind (string $ dn , string $ password , array $ sasl_args = null ): ? bool
520526 {
521527 if ($ sasl_args != null ) {
522528 if (!function_exists ('ldap_sasl_bind ' )) {
@@ -580,7 +586,7 @@ public function bind($dn, $password, array $sasl_args = null)
580586 * @param mixed $value
581587 * @return void
582588 */
583- public function setOption ($ option , $ value )
589+ public function setOption ($ option , $ value ): void
584590 {
585591 // Attempt to set the LDAP option
586592 if (!@ldap_set_option ($ this ->ldap , $ option , $ value )) {
@@ -615,7 +621,7 @@ public function setOption($option, $value)
615621 * The array of attributes and their values.
616622 * @see http://no.php.net/manual/en/function.ldap-read.php
617623 */
618- public function getAttributes ($ dn , $ attributes = null , $ maxsize = null )
624+ public function getAttributes (string $ dn , $ attributes = null , int $ maxsize = null ): array
619625 {
620626 // Preparations, including a pretty debug message...
621627 $ description = 'all attributes ' ;
@@ -698,10 +704,10 @@ public function getAttributes($dn, $attributes = null, $maxsize = null)
698704 *
699705 * @param array $config
700706 * @param string $username
701- * @param string $password
707+ * @param string|null $password
702708 * @return array|false
703709 */
704- public function validate ($ config , $ username , $ password = null )
710+ public function validate (array $ config , string $ username , string $ password = null )
705711 {
706712 /**
707713 * Escape any characters with a special meaning in LDAP. The following
@@ -755,7 +761,7 @@ public function validate($config, $username, $password = null)
755761 * @param bool $singleValue
756762 * @return string|array Array $values, but escaped
757763 */
758- public static function escapeFilterValue ($ values = [], $ singleValue = true )
764+ public static function escapeFilterValue ($ values = [], bool $ singleValue = true )
759765 {
760766 // Parameter validation
761767 $ values = Utils \Arrays::arrayize ($ values );
@@ -793,7 +799,7 @@ public static function escapeFilterValue($values = [], $singleValue = true)
793799 * @static
794800 * @return string
795801 */
796- public static function asc2hex32 ($ string )
802+ public static function asc2hex32 (string $ string ): string
797803 {
798804 for ($ i = 0 ; $ i < strlen ($ string ); $ i ++) {
799805 $ char = substr ($ string , $ i , 1 );
@@ -808,6 +814,7 @@ public static function asc2hex32($string)
808814 return $ string ;
809815 }
810816
817+
811818 /**
812819 * Convert SASL authz_id into a DN
813820 *
@@ -816,7 +823,7 @@ public static function asc2hex32($string)
816823 * @param string $authz_id
817824 * @return string|null
818825 */
819- private function authzidToDn ($ searchBase , $ searchAttributes , $ authz_id )
826+ private function authzidToDn (string $ searchBase , array $ searchAttributes , string $ authz_id ): ? string
820827 {
821828 if (preg_match ("/^dn:/ " , $ authz_id )) {
822829 return preg_replace ("/^dn:/ " , "" , $ authz_id );
@@ -832,46 +839,24 @@ private function authzidToDn($searchBase, $searchAttributes, $authz_id)
832839 return $ authz_id ;
833840 }
834841
842+
835843 /**
836844 * ldap_exop_whoami accessor, if available. Use requested authz_id
837845 * otherwise.
838846 *
839- * ldap_exop_whoami() has been provided as a third party patch that
840- * waited several years to get its way upstream:
841- * http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/databases/php-ldap/files
842- *
843- * When it was integrated into PHP repository, the function prototype
844- * was changed, The new prototype was used in third party patch for
845- * PHP 7.0 and 7.1, hence the version test below.
846- *
847847 * @param string $searchBase
848848 * @param array $searchAttributes
849849 * @throws \Exception
850850 * @return string
851851 */
852- public function whoami ($ searchBase , $ searchAttributes )
852+ public function whoami (string $ searchBase , array $ searchAttributes ): string
853853 {
854- $ authz_id = '' ;
855- if (function_exists ('ldap_exop_whoami ' )) {
856- if (version_compare (phpversion (), '7 ' , '< ' )) {
857- /** @psalm-suppress TooManyArguments */
858- if (ldap_exop_whoami ($ this ->ldap , $ authz_id ) === false ) {
859- throw $ this ->makeException ('LDAP whoami exop failure ' );
860- }
861- } else {
862- $ authz_id = ldap_exop_whoami ($ this ->ldap );
863- if ($ authz_id === false ) {
864- throw $ this ->makeException ('LDAP whoami exop failure ' );
865- }
866- }
867- } else {
868- Assert::string ($ authz_id );
869- /** @var string $authz_id */
870- $ authz_id = $ this ->authz_id ;
854+ $ authz_id = ldap_exop_whoami ($ this ->ldap );
855+ if ($ authz_id === false ) {
856+ throw $ this ->makeException ('LDAP whoami exop failure ' );
871857 }
872858
873859 $ dn = $ this ->authzidToDn ($ searchBase , $ searchAttributes , $ authz_id );
874-
875860 if (empty ($ dn )) {
876861 throw $ this ->makeException ('Cannot figure userID ' );
877862 }
0 commit comments