<commit_before><?php

/**
 * @copyright   Copyright (c) 2015 ublaboo <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @package     Ublaboo
 */

namespace Ublaboo\DataGrid\AggregationFunction;

use DibiFluent;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder;
use Nette\Utils\Strings;
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;

final class FunctionSum implements IAggregationFunction
{
	/**
	 * @var string
	 */
	private $column;

	/**
	 * @var int
	 */
	private $result = 0;

	/**
	 * @var int
	 */
	private $dataType;

	/**
	 * @var callable
	 */
	private $renderer;


	/**
	 * @param string $column
	 * @param int $dataType
	 */
	public function __construct($column, $dataType = IAggregationFunction::DATA_TYPE_PAGINATED)
	{
		$this->column = $column;
		$this->dataType = $dataType;
	}


	/**
	 * @return bool
	 */
	public function getFilterDataType()
	{
		return $this->dataType;
	}


	/**
	 * @param  mixed  $dataSource
	 * @return void
	 */
	public function processDataSource($dataSource)
	{
		if ($dataSource instanceof DibiFluent) {
			$connection = $dataSource->getConnection();
			$this->result = $connection->select('SUM(%n) AS sum', $this->column)
				->from($dataSource, 's')
				->fetch()
				->sum;
		}

		if ($dataSource instanceof QueryBuilder) {
			$column = Strings::contains($this->column, '.')
				? $this->column
				: current($dataSource->getRootAliases()).'.'.$this->column;

			$this->result = $dataSource
				->select(sprintf('SUM(%s)', $column))
				->getQuery()
				->getSingleScalarResult();
		}

		if ($dataSource instanceof Collection) {
			$dataSource->forAll(function ($key, $value) {
				$this->result += PropertyAccessHelper::getValue($value, $this->column);
				return TRUE;
			});
		}
	}


	/**
	 * @return mixed
	 */
	public function renderResult()
	{
		$result = $this->result;

		if (isset($this->renderer)) {
			$result = call_user_func($this->renderer, $result);
		}

		return $result;
	}


	/**
	 * @param  callable|NULL  $callback
	 * @return static
	 */
	public function setRenderer(callable $callback = NULL)
	{
		$this->renderer = $callback;
		return $this;
	}
}
<commit_msg>Removed unwanted final keyword from SUM Aggregation<commit_after><?php

/**
 * @copyright   Copyright (c) 2015 ublaboo <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @package     Ublaboo
 */

namespace Ublaboo\DataGrid\AggregationFunction;

use DibiFluent;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder;
use Nette\Utils\Strings;
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;

class FunctionSum implements IAggregationFunction
{
	/**
	 * @var string
	 */
	protected $column;

	/**
	 * @var int
	 */
	protected $result = 0;

	/**
	 * @var int
	 */
	protected $dataType;

	/**
	 * @var callable
	 */
	protected $renderer;


	/**
	 * @param string $column
	 * @param int $dataType
	 */
	public function __construct($column, $dataType = IAggregationFunction::DATA_TYPE_PAGINATED)
	{
		$this->column = $column;
		$this->dataType = $dataType;
	}


	/**
	 * @return bool
	 */
	public function getFilterDataType()
	{
		return $this->dataType;
	}


	/**
	 * @param  mixed  $dataSource
	 * @return void
	 */
	public function processDataSource($dataSource)
	{
		if ($dataSource instanceof DibiFluent) {
			$connection = $dataSource->getConnection();
			$this->result = $connection->select('SUM(%n) AS sum', $this->column)
				->from($dataSource, 's')
				->fetch()
				->sum;
		}

		if ($dataSource instanceof QueryBuilder) {
			$column = Strings::contains($this->column, '.')
				? $this->column
				: current($dataSource->getRootAliases()).'.'.$this->column;

			$this->result = $dataSource
				->select(sprintf('SUM(%s)', $column))
				->getQuery()
				->getSingleScalarResult();
		}

		if ($dataSource instanceof Collection) {
			$dataSource->forAll(function ($key, $value) {
				$this->result += PropertyAccessHelper::getValue($value, $this->column);
				return TRUE;
			});
		}
	}


	/**
	 * @return mixed
	 */
	public function renderResult()
	{
		$result = $this->result;

		if (isset($this->renderer)) {
			$result = call_user_func($this->renderer, $result);
		}

		return $result;
	}


	/**
	 * @param  callable|NULL  $callback
	 * @return static
	 */
	public function setRenderer(callable $callback = NULL)
	{
		$this->renderer = $callback;
		return $this;
	}
}

{'fraction_non_alphanumeric': 0.16451612903225807, 'fraction_numerical': 0.002304147465437788, 'mean_word_length': 3.4845041322314048, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 63, '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': '354299', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1433, 'n_words': 295}