<commit_before><?php
/*
 * Created by <NAME> on 2009-08-08.
 * Copyright (c) 2009 <NAME>.
 * All rights reserved.
 */

/**
 * Uses the Composite pattern to construct code.
 */
abstract class Db_Mapper_CodeContainer
{
	/**
	 * Array of nested parts.
	 * 
	 * @var array
	 */
	protected $content = array();
	
	// ------------------------------------------------------------------------

	/**
	 * Returns the name / identifier of this CodeContainer.
	 * 
	 * @return string
	 */
	abstract public function getName();
	
	// ------------------------------------------------------------------------

	/**
	 * Adds a part to this container.
	 * 
	 * @param  Db_Mapper_CodeContainer
	 * @param  string	The path to the destination container
	 * @param  bool
	 * @return bool
	 */
	final public function addPart($part, $path = '', $replace = false)
	{
		if( ! is_string($part) && ! $part instanceof Db_Mapper_CodeContainer)
		{
			throw new InvalidArgumentException(is_object($part) ? get_class($part) : gettype($part));
		}
		
		if(empty($part))
		{
			return true;
		}
		
		if(empty($path))
		{
			if($replace && $part instanceof Db_Mapper_CodeContainer)
			{
				// replace the CodeContainer with a new one
				foreach($this->content as $k => $p)
				{
					// matching names
					if($part->getName() == $p->getName())
					{
						unset($this->content[$k]);
					}
				}
			}
			
			$this->content[] = $part;
			
			return true;
		}
		else
		{
			// get the first dot to separate segments
			$p = ($p = strpos($path, '.')) === false ? strlen($path) : $p;
			
			// get key and then the rest of it
			$key = substr($path, 0, $p);
			$path = substr($path, $p + 1);
			
			// return variable
			$part_added = false;
			
			// find the container to add it to
			foreach($this->content as $container)
			{
				if($container instanceof Db_Mapper_CodeContainer)
				{
					if($container->getName() != $key)
					{
						continue;
					}
					
					$part_added = $part_added OR $container->addPart($part, $path);
				}
			}
			
			return $part_added;
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Removes a code part from this container.
	 * 
	 * @param  string	The path to the container to remove (includes the container to remove)
	 * @return bool
	 */
	public function removePart($path)
	{<commit_msg>Now forces the Db_Mapper_CodeContainer to call addPart when using paths, even if it has added it to another earlier Fixed bug with how PHP parses $c = $a OR $b; (it parses it like this: ($c = $a) OR $b)<commit_after><?php
/*
 * Created by <NAME> on 2009-08-08.
 * Copyright (c) 2009 <NAME>.
 * All rights reserved.
 */

/**
 * Uses the Composite pattern to construct code.
 */
abstract class Db_Mapper_CodeContainer
{
	/**
	 * Array of nested parts.
	 * 
	 * @var array
	 */
	protected $content = array();
	
	// ------------------------------------------------------------------------

	/**
	 * Returns the name / identifier of this CodeContainer.
	 * 
	 * @return string
	 */
	abstract public function getName();
	
	// ------------------------------------------------------------------------

	/**
	 * Adds a part to this container.
	 * 
	 * @param  Db_Mapper_CodeContainer
	 * @param  string	The path to the destination container
	 * @param  bool
	 * @return bool
	 */
	final public function addPart($part, $path = '', $replace = false)
	{
		if( ! is_string($part) && ! $part instanceof Db_Mapper_CodeContainer)
		{
			throw new InvalidArgumentException(is_object($part) ? get_class($part) : gettype($part));
		}
		
		if(empty($part))
		{
			return true;
		}
		
		if(empty($path))
		{
			if($replace && $part instanceof Db_Mapper_CodeContainer)
			{
				// replace the CodeContainer with a new one
				foreach($this->content as $k => $p)
				{
					// matching names
					if($part->getName() == $p->getName())
					{
						unset($this->content[$k]);
					}
				}
			}
			
			$this->content[] = $part;
			
			return true;
		}
		else
		{
			// get the first dot to separate segments
			$p = ($p = strpos($path, '.')) === false ? strlen($path) : $p;
			
			// get key and then the rest of it
			$key = substr($path, 0, $p);
			$path = substr($path, $p + 1);
			
			// return variable
			$part_added = false;
			
			// find the container to add it to
			foreach($this->content as $container)
			{
				if($container instanceof Db_Mapper_CodeContainer)
				{
					if($container->getName() != $key)
					{
						continue;
					}
					
					$r = $container->addPart($part, $path, $replace);
					
					$part_added = ($part_added OR $r);
				}
			}
			
			return $part_added;
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Removes a code part from this container.
	 * 
	 * @param  string	The path to the container to remove (includes the container to remove)
	 * @return bool
	 */
	public function removePart($path)
	{
{'fraction_non_alphanumeric': 0.21739130434782608, 'fraction_numerical': 0.005715452133088385, 'mean_word_length': 1.8671737858396724, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '4988036', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1418, 'n_words': 426}