PHP 5.3避免在foreach循环中嵌套try/catch重复(代码三明治)


php 5.3 avoid try/catch duplication nested within foreach loop (code sandwich)

我有一个methodA类,它的现有结构如下

function methodA() {
  $providers = $this->getFirstSetOfProviders();
  foreach ($providers as $provider) {
    try {
      $this->method1($provider);
    } catch ( Exception $e ) {
      // exception handling
    }
  }
  $providers = $this->getSecondSetOfProviders();
  foreach ($providers as $provider) {
    try {
      $this->method2($provider);
    } catch ( Exception $e ) {
      // exception handling
    }
  }
}

catch子句的内容相同。是否有一些方法来组织代码,以避免重复嵌套在foreach循环中的try/catch结构?从概念上讲,我想做

function methodA() {
  foreach ($providers as $provider) {
    $method1 = function($provider) {
      $this->method1($provider);
    }
    $this->withTryCatch($method1);
  }
  ...
}
function withTryCatch($method) {
  try {
    $method;  // invoke this method somehow
  } catch (Exception $e) {
    // exception handling
  }
}

这看起来类似于代码三明治,但我不确定如何在php中进行。

更新:try/catch嵌套在foreach循环中,因此当抛出异常时,它会被处理,并继续执行循环中的下一个迭代,而不是终止循环。

异常的好处是它们是可以像其他对象一样传递的对象。因此,您可以删除重复的代码(除了基本的样板),而无需做太多更改:

foreach ($providers as $provider) {
    try {
      $this->method1($provider);
    } catch ( Exception $e ) {
      $this->handleException($e);
    }
}

注意:如果在异常处理中还需要一些上下文(即$provider),只需给handleException()更多的参数。

Part 2:重构整个方法

您想知道如何进一步删除重复。我不知道这在您的实际代码中是否有意义,它也可能是过度工程。这件事你得自己决定。下面是模板方法模式的实现。请原谅我粗鲁的命名,但我试着以你为榜样,我不知道你在做什么。

abstract class ClassThatDoesThingsWithProviders
{
    public function methodA($providers)
    {
        foreach($provicers as $provider) {
            try {
                $this->methodThatActuallyDoesSomethingWithProvider($provider);
            } catch(Exception $e) {
                $this->handleException($e);
            }
        }
    }
    protected function handleException(Exception $e)
    {
        // handle exception
    }
    abstract protected function methodThatActuallyDoesSomethingWithProvider($provider);
}
class ClassThatDoesThing1WithProviders extends ClassThatDoesThingsWithProviders
{
    protected function methodThatActuallyDoesSomethingWithProvider($provider)
    {
        // this is your method1()
    }
}
class ClassThatDoesThing2WithProviders extends ClassThatDoesThingsWithProviders
{
    protected function methodThatActuallyDoesSomethingWithProvider($provider)
    {
        // this is your method2()
    }
}
class YourOriginalClass
{
    protected $thingsdoer1;
    protected $thingsdoer2;
    public function __construct()
    {
        $this->thingsdoer1 = new ClassThatDoesThing1WithProviders;
        $this->thingsdoer2 = new ClassThatDoesThing2WithProviders;
    }
    public function methodA()
    {
        $this->thingsdoer1->methodA($this->getFirstSetOfProviders());
        $this->thingsdoer2->methodA($this->getSecondSetOfProviders());
    }
}

你可以很容易地把thingsdoer1thingsdoer2组成一个数组,也可以把getFirstSetOfProvidersgetSecondSetOfProviders抽象在一起。我也不知道实际的method1和method2的实现依赖于什么,也许你不能像那样提取它们而不破坏内聚。

但由于我不了解你的实际代码和你在做什么,我无法推荐一个具体的策略,以我上面的例子为起点。

function methodA() {
  try {
      $providers = $this->getFirstSetOfProviders();
      foreach ($providers as $provider) {
          $this->method1($provider);
      }
      $providers = $this->getSecondSetOfProviders();
      foreach ($providers as $provider) {
          $this->method2($provider);
      }
  } catch ( Exception $e ) {
    // exception handling
  }
}