At a minimum, recycle it so that it reads what you said it should do:
It is supposed to return true, if the status is set as one of these OTHERWISE company names, if logintype is an administrator, it should also return true regardless of company or status
function mayAccessResource(array $sessionState) { return (hasStatus($sessionState) && isAllowedUserOrCompany($sessionState)) || isAdmin($sessionState); }
Then add methods to encapsulate single tests:
function hasStatus($sessionState) { return isset($sessionState['status']); } function isAllowedUserOrCompany($sessionState) { return isAllowedUser($sessionState) || isAllowedCompany($sessionState); } function isAllowedUser($sessionState) { return isset($sessionState['user']) && $sessionState['user'] === 'blah'; } function isAllowedCompany($sessionState) { $allowedCompanies = array('foo', 'baz', 'baz'); return isset($sessionState['company']) && in_array($sessionState['company'], $allowedCompanies); } function isAdmin($sessionState) { return isset($sessionState['LOGINTYPE']) && $sessionState['LOGINTYPE'] === 'admin'; }
This is still far from perfect, because it hardcodes too much information, but at least now it is much more readable. Since they all work in session state, it would be much more beautiful to have a SessionState object. It would be even better to tease the various possible session states further apart and have a Company, User, and AccessControl class.
Also pay attention to the comments below your question.
source share