Lowering the cyclomatic complexity of this method, suggestions? The PHP Mess Detector of my IDE warns about a Cyclomatic Complexity of 14 (threshold is 10). To me this code doesn't seem to hard to follow. Would you refactor it in some way to lower the metric?


Exit if $payload isn't an array or has a number of elements not equal to 1
If $payload is array or ArrayAccess build a proper checker/setter
If $payload is stdClass do the same as 2
Use control options and checker/setter to actually set the value inside $payload


The method:

public function onBeforeEncrypt(CryptDecryptEvent $event)
{
    // Get the actual parameters of the call
    $payload = $event-&gt;getPayload();

    // Avoid errors when __soapCall is invoked directly with custom arguments
    if (!is_array($payload) || 1 !== count($payload)) {
        return;
    }

    // Reference to the payload (by reference just in case it's an array)
    $payload = &amp;$payload[0];

    // Shortcuts to control options
    $paramName      = $this-&gt;options['name'];
    $paramValue     = $this-&gt;options['value'];
    $allowOverride  = $this-&gt;options['allowOverride'];
    $nullLikeNotSet = $this-&gt;options['threatNullLikeNotSet'];

    // Dynamically build checker and setter based on the type
    $checker = $setter = null;

    // Payload is array or ArrayAccess implementation inside main array
    if (is_array($payload) || $payload instanceof \ArrayAccess) {
        $checker = function ($k) use ($payload, $nullLikeNotSet) {
            return isset($payload[$k]) || (null === $payload[$k] &amp;&amp; $nullLikeNotSet);
        };

        $setter = function ($k, $v) use (&amp;$payload) { $payload[$k] = $v; };
    }

    // Payload is stdClass inside the main array
    if ($payload instanceof \stdClass) {
        $checker = function ($p) use ($payload, $nullLikeNotSet) {
            return isset($payload-&gt;$p) || (null === $payload-&gt;$p &amp;&amp; $nullLikeNotSet);
        };

        $setter = function ($p, $v) use ($payload) { $payload-&gt;$p = $v; };
    }

    // Set the value if we can override or parameter is abset
    if ($checker &amp;&amp; $setter &amp;&amp; ($allowOverride || !$checker($paramName)) {
        $setter($paramName, $paramValue);
    }
}
You could easily extract the 4 functions you have within if statements by having

arrayChecker()
arraySetter()
classChecker()
arraySetter()


And then have something like 

//Exit immediately if override is not allowed ( why wait ? )
//Personally, that would be the first line for me
if(!$allowOverride)
  return;

// Payload is array or ArrayAccess implementation inside main array
if (is_array($payload) || $payload instanceof \ArrayAccess) {
  if(!$arrayChecker($paramName)) {
    $arraySetter($paramName, $paramValue);
  }
}

// Payload is stdClass inside the main array
if ($payload instanceof \stdClass) {
  if(!$classChecker($paramName)) {
    $classSetter($paramName, $paramValue);
  }
}  


One more thing, you re-use the payload variable. For me, what you get from ->getPayload() is a payload of an array with something in the first position. ( crate plus the goods ). To then re-use the variable to point just to the goods seems wrong to me.