<commit_before><?php
namespace Headzoo\Core;

/**
 * Used to call a function when the object destructs.
 *
 * The class wraps a callable function, which is called in the class destructor. The utility
 * of this scheme is the ability to ensure the function is called eventually. Usually when
 * the SmartCallable object goes out of scope, which is when it's destructor is called.
 * 
 * This class can be used to simulate a try...catch...finally in versions of PHP which do not
 * support the finally clause.
 * 
 * Example:
 * ```php
 * // In this example we create a method which requests a web resource using curl.
 * // We use a SmartCallable instance to ensure the curl resource is closed when
 * // the method returns, or an exception is thrown.
 * public function fetch()
 * {
 *      $curl = curl_init("http://some-site.com");
 *      $sc = SmartCallable::factory(function() use($curl) {
 *              curl_close($curl);
 *      });
 * 
 *      return curl_exec($curl);
 * }
 * ```
 * 
 * @see http://blogs.balabit.com/2011/02/20/try-catch-finally-in-php/
 */
class SmartCallable
    extends Obj
{
    /**
     * Function to be called in the destructor
     * @var callable
     */
    private $callable;

    /**
     * Arguments passed to the callable
     * @var array
     */
    private $args = [];

    /**
     * Static factory class to create a new instance of this class
     *
     * Example:
     * ```php
     * $sc = SmartCallable::factory(function() {
     *      echo "I'm complete!";<commit_msg>Updated phpdoc<commit_after><?php
namespace Headzoo\Core;

/**
 * Used to call a function when a resource is no longer needed.
 *
 * SmartCallable instances wrap functions, which are called when the SmartCallable instances
 * go out of scope. The idea is similar to "smart pointers" from C++, where an object -- the smart
 * pointer -- wraps a resource, and automatically frees the resource when it's no longer needed.
 *
 * In this example we create a method which requests a web resource using curl.
 * We use a SmartCallable instance to ensure the curl resource is closed when
 * the method returns, or an exception is thrown.
 * 
 * ```php
 * public function fetch()
 * {
 *      $curl = curl_init("http://some-site.com");
 *      $sc = SmartCallable::factory(function() use($curl) {
 *              curl_close($curl);
 *      });
 * 
 *      $response = curl_exec($curl);
 *      if ($e = curl_error()) {
 *          throw new Exception($e);
 *      }
 * 
 *      return $response;
 * }
 * ```
 * 
 * The method could also be written this way.
 * 
 * ```php
 * public function fetch()
 * {
 *      $curl = curl_init("http://some-site.com");
 *      $sc = SmartCallable::factory("curl_close", $curl);
 *
 *      $response = curl_exec($curl);
 *      if ($e = curl_error()) {
 *          throw new Exception($e);
 *      }
 *
 *      return $response;
 * }
 * ```
 * This class can be used to simulate a try...catch...finally in versions of PHP which do not
 * support the finally clause. In this example the database connection is closed even if we
 * throw an exception before calling the close() method.
 * 
 * ```php
 * public function fetchRows()
 * {
 *      $mysqli = new mysqli('localhost', 'my_user', 'my_password', '<PASSWORD>');
 *      $sc = SmartCallable::factory(function() use($mysql) {
 *          $mysqli->close();
 *      });
 * 
 *      try {
 *          $result = $mysqli->query("SELECT Name FROM City LIMIT 10");
 *      } catch (Exception $e) {
 *          $this->logger->error($e->getMessage());
 *          throw $e;
 *      }
 * 
 *      $mysql->close();
 *      // We could also close the connection by invoking the SmartCallable object.
 *      $sc();
 * 
 *      return $result;
 * }
 * ```
 * 
 * @see http://blogs.balabit.com/2011/02/20/try-catch-finally-in-php/
 */
class SmartCallable
    extends Obj
{
    /**
     * Function to be called in the destructor
     * @var callable
     */
    private $callable;

    /**
     * Arguments passed to the callable
     * @var array
     */
    private $args = [];

    /**
     * Static factory class to create a new instance of this class
     *
     * Example:
     * ```php
     * $sc = SmartCallable::factory(function() {
     *      echo "I'm complete!";
{'fraction_non_alphanumeric': 0.1305470767785865, 'fraction_numerical': 0.004226344212256398, 'mean_word_length': 2.457792207792208, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7301026', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1235, 'n_words': 434}