| Conditions | 7 |
| Paths | 24 |
| Total Lines | 95 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 72 | protected function login(string $username, string $password): array |
||
| 73 | { |
||
| 74 | $encryption = $this->ldapConfig->getString('encryption', 'ssl'); |
||
| 75 | Assert::oneOf($encryption, ['none', 'ssl', 'tls']); |
||
| 76 | |||
| 77 | $version = $this->ldapConfig->getInteger('version', 3); |
||
| 78 | Assert::positiveInteger($version); |
||
| 79 | |||
| 80 | $ldapServers = []; |
||
| 81 | foreach (explode(' ', $this->ldapConfig->getString('connection_string')) as $connection_string) { |
||
| 82 | Assert::regex($connection_string, '#^ldap[s]?:\/\/#'); |
||
| 83 | |||
| 84 | $ldapServers[] = LdapObject::create( |
||
| 85 | $this->ldapConfig->getString('extension', 'ext_ldap'), |
||
| 86 | [ |
||
| 87 | 'connection_string' => $connection_string, |
||
| 88 | 'encryption' => 'ssl', |
||
| 89 | 'version' => $version, |
||
| 90 | ] |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | $searchScope = $this->ldapConfig->getString('search.scope', Query::SCOPE_SUB); |
||
| 95 | Assert::oneOf($searchScope, [Query::SCOPE_BASE, Query::SCOPE_ONE, Query::SCOPE_SUB]); |
||
| 96 | |||
| 97 | $referrals = $this->ldapConfig->getValue('referrals', Query::DEREF_NEVER); |
||
| 98 | Assert::oneOf($referrals, [Query::DEREF_ALWAYS, Query::DEREF_NEVER, Query::DEREF_FINDING, Query::DEREF_SEARCHING]); |
||
| 99 | |||
| 100 | $timeout = $this->ldapConfig->getString('timeout', 3); |
||
| 101 | $searchBase = $this->ldapConfig->getArray('search.base'); |
||
| 102 | $options = [ |
||
| 103 | 'scope' => $searchScope, |
||
| 104 | 'timeout' => $timeout, |
||
| 105 | 'deref' => $referrals, |
||
| 106 | ]; |
||
| 107 | |||
| 108 | $searchEnable = $this->ldapConfig->getBoolean('search.enable', false); |
||
| 109 | if ($searchEnable === false) { |
||
| 110 | $dnPattern = $this->ldapConfig->getString('dnpattern'); |
||
| 111 | $dn = str_replace('%username%', $username, $dnPattern); |
||
| 112 | |||
| 113 | $filter = ''; |
||
|
|
|||
| 114 | } else { |
||
| 115 | $searchUsername = $this->ldapConfig->getString('search.username'); |
||
| 116 | Assert::notWhitespaceOnly($searchUsername); |
||
| 117 | |||
| 118 | $searchPassword = $this->ldapConfig->getString('search.password', null); |
||
| 119 | Assert::nullOrnotWhitespaceOnly($searchPassword); |
||
| 120 | |||
| 121 | $searchAttributes = $this->ldapConfig->getArray('search.attributes'); |
||
| 122 | $searchFilter = $this->ldapConfig->getString('search.filter', null); |
||
| 123 | |||
| 124 | $ldap = $this->bind($ldapServers, $searchUsername, $searchPassword); |
||
| 125 | |||
| 126 | $filter = ''; |
||
| 127 | foreach ($searchAttributes as $attr) { |
||
| 128 | $filter .= '(' . $attr . '=' . $username . ')'; |
||
| 129 | } |
||
| 130 | $filter = '(|' . $filter . ')'; |
||
| 131 | |||
| 132 | // Append LDAP filters if defined |
||
| 133 | if ($searchFilter !== null) { |
||
| 134 | $filter = "(&" . $filter . $searchFilter . ")"; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** @psalm-var \Symfony\Component\Ldap\Entry $entry */ |
||
| 138 | $entry = $this->search($ldap, $searchBase, $filter, $options, false); |
||
| 139 | $dn = $entry->getDn(); |
||
| 140 | } |
||
| 141 | |||
| 142 | $ldap = $this->bind($ldapServers, $dn, $password); |
||
| 143 | $filter = sprintf('(distinguishedName=%s)', $dn); |
||
| 144 | |||
| 145 | /** @psalm-var \Symfony\Component\Ldap\Entry $entry */ |
||
| 146 | $entry = $this->search($ldap, $searchBase, $filter, $options, false); |
||
| 147 | |||
| 148 | $attributes = $this->ldapConfig->getArray('attributes', []); |
||
| 149 | if ($attributes === ['*']) { |
||
| 150 | $result = $entry->getAttributes(); |
||
| 151 | } else { |
||
| 152 | $result = array_intersect_key( |
||
| 153 | $entry->getAttributes(), |
||
| 154 | array_fill_keys(array_values($attributes), null) |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | $binaries = array_intersect( |
||
| 159 | array_keys($result), |
||
| 160 | $this->ldapConfig->getArray('attributes.binary', []), |
||
| 161 | ); |
||
| 162 | foreach ($binaries as $binary) { |
||
| 163 | $result[$binary] = array_map('base64_encode', $result[$binary]); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $result; |
||
| 167 | } |
||
| 256 |