<commit_before><?php
namespace Swagger;

use Swagger\Object as SwaggerObject;
use Swagger\Exception as SwaggerException;

class Document extends SwaggerObject\AbstractObject
{
    protected $defaultScheme;

    protected $operationsById = [];
    
    protected $schemaResolver;

    public function getOperationsById($reset = false)
    {
        if($reset) {
            $this->operationsById = [];
        }
        
        if(empty($this->operationsById)) {
            $paths = $this->getPaths();
            foreach($paths->getAll() as $path => $pathItem) {
                foreach([
                    [$pathItem, 'getGet'],
                    [$pathItem, 'getPut'],
                    [$pathItem, 'getPost'],
                    [$pathItem, 'getDelete'],
                    [$pathItem, 'getOptions'],
                    [$pathItem, 'getHead'],
                    [$pathItem, 'getPatch'],
                ] as $operationMethod) {
                    try {
                        $operation = $operationMethod();
                        
                        if($operation instanceof SwaggerObject\Operation) {
                            $operationKey = strtolower($operation->getOperationId());
                        
                            $this->operationsById[$operationKey] = new OperationReference(
                                $operation->getOperationId(),
                                $path,
                                $pathItem,
                                strtoupper(substr($operationMethod[1], 3)),
                                $operation
                            );
                        }
                    } catch(SwaggerException\MissingDocumentPropertyException $e) {
                        // That's okay. Not every method is implemented.
                    }
                }
            }
        }
        
        return $this->operationsById;
    }
    
    public function getOperationById($operationId, $reset = false)
    {
        $operations = $this->getOperationsById($reset);
        $operationId = strtolower($operationId);
        
        if(!isset($operations[$operationId])) {
            throw new SwaggerException\InvalidOperationException('Operation by the specified ID does not exist');
        }
        
        return $operations[$operationId];
    }
    
    public function getSecuritySchemeOfType($type)
    {
        $securityDefinitions = $this->getSecurityDefinitions();
        $schemes = $securityDefinitions->getAll();
        $schemeOfType = null;
        
        foreach($schemes as $name => $scheme) {
            if($scheme->getType() === $type) {
                $schemeOfType = $scheme;
                break;
            }
        }
        
        if(empty($scheme)) {
            throw new \DomainException("No security schemes of type '{$type}' are defined on this API");
        }
        
        return $scheme;
    }
    
    public function getSchemaForOperationResponse(
        $operationId,
        $statusCode
    )
    {
        $operation = $this->getOperationById($operationId);
        try {
            $schema = $operation->getResponses()
                ->getHttpStatusCode($statusCode);
        } catch(SwaggerException\MissingDocumentPropertyException $e) {
            // This status is not defined, but we can hope for an operation default
            try {
                $schema = $operation->getResponse()
                    ->getDefault();
            } catch(SwaggerException\MissingDocumentPropertyException $e) {
                throw new \UnexpectedValueException("No schema can be found for operation '{$operationId}'");
            }
        }
        
        return $schema;
    }
    
    public function getDefaultScheme()
    {
        if(!$this->defaultScheme) {
            $schemes = $this->getSchemes();
            $defaultScheme = reset($schemes);
            
            if($defaultScheme) {
                $this->defaultScheme = $defaultScheme;
            }
        }
    
        return $this->defaultScheme;
    }
    
    public function setDefaultScheme($defaultScheme)
    {<commit_msg>Grab the operation itself off the OperationReference.<commit_after><?php
namespace Swagger;

use Swagger\Object as SwaggerObject;
use Swagger\Exception as SwaggerException;

class Document extends SwaggerObject\AbstractObject
{
    protected $defaultScheme;

    protected $operationsById = [];
    
    protected $schemaResolver;

    public function getOperationsById($reset = false)
    {
        if($reset) {
            $this->operationsById = [];
        }
        
        if(empty($this->operationsById)) {
            $paths = $this->getPaths();
            foreach($paths->getAll() as $path => $pathItem) {
                foreach([
                    [$pathItem, 'getGet'],
                    [$pathItem, 'getPut'],
                    [$pathItem, 'getPost'],
                    [$pathItem, 'getDelete'],
                    [$pathItem, 'getOptions'],
                    [$pathItem, 'getHead'],
                    [$pathItem, 'getPatch'],
                ] as $operationMethod) {
                    try {
                        $operation = $operationMethod();
                        
                        if($operation instanceof SwaggerObject\Operation) {
                            $operationKey = strtolower($operation->getOperationId());
                        
                            $this->operationsById[$operationKey] = new OperationReference(
                                $operation->getOperationId(),
                                $path,
                                $pathItem,
                                strtoupper(substr($operationMethod[1], 3)),
                                $operation
                            );
                        }
                    } catch(SwaggerException\MissingDocumentPropertyException $e) {
                        // That's okay. Not every method is implemented.
                    }
                }
            }
        }
        
        return $this->operationsById;
    }
    
    public function getOperationById($operationId, $reset = false)
    {
        $operations = $this->getOperationsById($reset);
        $operationId = strtolower($operationId);
        
        if(!isset($operations[$operationId])) {
            throw new SwaggerException\InvalidOperationException('Operation by the specified ID does not exist');
        }
        
        return $operations[$operationId];
    }
    
    public function getSecuritySchemeOfType($type)
    {
        $securityDefinitions = $this->getSecurityDefinitions();
        $schemes = $securityDefinitions->getAll();
        $schemeOfType = null;
        
        foreach($schemes as $name => $scheme) {
            if($scheme->getType() === $type) {
                $schemeOfType = $scheme;
                break;
            }
        }
        
        if(empty($scheme)) {
            throw new \DomainException("No security schemes of type '{$type}' are defined on this API");
        }
        
        return $scheme;
    }
    
    public function getSchemaForOperationResponse(
        $operationId,
        $statusCode
    )
    {
        $operation = $this->getOperationById($operationId);
        try {
            $schema = $operation->getOperation()
                ->getResponses()
                ->getHttpStatusCode($statusCode);
        } catch(SwaggerException\MissingDocumentPropertyException $e) {
            // This status is not defined, but we can hope for an operation default
            try {
                $schema = $operation->getResponse()
                    ->getDefault();
            } catch(SwaggerException\MissingDocumentPropertyException $e) {
                throw new \UnexpectedValueException("No schema can be found for operation '{$operationId}'");
            }
        }
        
        return $schema;
    }
    
    public function getDefaultScheme()
    {
        if(!$this->defaultScheme) {
            $schemes = $this->getSchemes();
            $defaultScheme = reset($schemes);
            
            if($defaultScheme) {
                $this->defaultScheme = $defaultScheme;
            }
        }
    
        return $this->defaultScheme;
    }
    
    public function setDefaultScheme($defaultScheme)
    {
{'fraction_non_alphanumeric': 0.10273237083880206, 'fraction_numerical': 0.0004772700155112755, 'mean_word_length': 1.4508771929824562, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, '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': '4705739', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1910, 'n_words': 397}