### Setup Ollama Backend Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/installation.md Commands to install, pull a model, and verify the status of a local Ollama server for token entropy computation. ```bash curl -fsSL https://ollama.com/install.sh | sh ollama pull llama3 ollama serve curl http://localhost:11434/api/tags ``` -------------------------------- ### Configure CI/CD Pipelines for LM-CC Analysis Source: https://context7.com/k-kinzal/lm-cc-php/llms.txt Provides configuration examples for running LM-CC analysis in automated pipelines. Includes setup for GitHub Actions and GitLab CI, utilizing Ollama as a service container for LLM-based analysis. ```yaml # .github/workflows/lm-cc.yml jobs: lm-cc: runs-on: ubuntu-latest services: ollama: image: ollama/ollama:latest ports: - 11434:11434 steps: - uses: actions/checkout@v4 - name: Run LM-CC analysis run: vendor/bin/lm-cc analyze src/ --threshold 50.0 ``` ```yaml # .gitlab-ci.yml lm-cc: stage: quality image: php:8.2 services: - name: ollama/ollama:latest alias: ollama script: - vendor/bin/lm-cc analyze src/ --threshold 50.0 --format json ``` -------------------------------- ### Configure OpenAI Backend Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/installation.md Configuration methods for using OpenAI as an LLM backend, including environment variable setup, YAML configuration, and CLI execution. ```bash export LMCC_API_KEY=sk-... ``` ```yaml backend: openai model: gpt-3.5-turbo-instruct base_url: "https://api.openai.com" ``` ```bash vendor/bin/lm-cc analyze src/ --backend openai --model gpt-3.5-turbo-instruct --api-key sk-... ``` -------------------------------- ### Install LM-CC via Composer Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/installation.md Instructions for installing the LM-CC package as a local project dependency or as a global command-line tool. ```bash composer require k-kinzal/lm-cc-php composer global require k-kinzal/lm-cc-php ``` -------------------------------- ### GitHub Actions CI for LM-CC PHP Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/ci-integration.md This configuration sets up a GitHub Actions workflow to run LM-CC. It checks out the code, sets up PHP, installs dependencies, pulls the Llama3 model via Ollama, and then runs the LM-CC analysis on the 'src/' directory with a threshold of 50.0. ```yaml name: LM-CC on: [push, pull_request] jobs: lm-cc: runs-on: ubuntu-latest services: ollama: image: ollama/ollama:latest ports: - 11434:11434 steps: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: php-version: '8.2' - run: composer install --no-interaction - run: | curl -s http://localhost:11434/api/pull -d '{"name":"llama3"}' - run: vendor/bin/lm-cc analyze src/ --threshold 50.0 ``` -------------------------------- ### GitLab CI for LM-CC PHP Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/ci-integration.md This GitLab CI configuration defines a job to run LM-CC. It uses a PHP 8.2 Docker image, sets up Ollama as a service, configures the base URL for Ollama, installs dependencies, and executes the LM-CC analysis. Failures are not allowed by default. ```yaml lm-cc: image: php:8.2 services: - name: ollama/ollama:latest alias: ollama variables: LMCC_BASE_URL: "http://ollama:11434" script: - composer install --no-interaction - vendor/bin/lm-cc analyze src/ --threshold 50.0 allow_failure: false ``` -------------------------------- ### Generic Bash Script for LM-CC Analysis Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/ci-integration.md A generic bash script to execute LM-CC analysis. It first installs Composer dependencies, then runs the LM-CC analysis command with JSON output format. The script checks the exit code to report specific errors or threshold exceedances. ```bash #!/bin/bash composer install --no-interaction vendor/bin/lm-cc analyze src/ --threshold 50.0 --format json exit_code=$? if [ $exit_code -eq 1 ]; then echo "LM-CC threshold exceeded!" exit 1 elif [ $exit_code -eq 2 ]; then echo "LM-CC analysis error!" exit 2 fi ``` -------------------------------- ### Manage Configuration with Config Class in PHP Source: https://context7.com/k-kinzal/lm-cc-php/llms.txt Explains how to use the Config class for managing application settings in PHP. It supports loading configurations from YAML files, environment variables, and command-line arguments, with a defined precedence order. The class allows easy access to configuration values and exclusion patterns. ```php 'openai', 'model' => 'gpt-3.5-turbo-instruct', 'api_key' => 'sk-your-key', 'threshold' => 50.0, 'format' => 'json', 'exclude' => ['vendor/*', 'tests/*', 'cache/*'], ]); // Access configuration values echo "Backend: " . $config->backend . "\n"; // 'ollama' or 'openai' echo "Model: " . $config->model . "\n"; // e.g., 'llama3' echo "Base URL: " . $config->baseUrl . "\n"; // API endpoint echo "Alpha: " . $config->alpha . "\n"; // 0.8 (default) echo "Percentile: " . $config->percentile . "\n"; // 67.0 (default) echo "Threshold: " . $config->threshold . "\n"; // null or float echo "Format: " . $config->format . "\n"; // 'text' or 'json' print_r($config->exclude); // ['vendor/*', 'tests/*'] ``` -------------------------------- ### CLI Command: lm-cc init Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/usage.md Generates a default configuration file for the project. ```APIDOC ## COMMAND: lm-cc init ### Description Generates a default `lm-cc.yaml` configuration file in the project root to manage analysis settings. ### Usage `vendor/bin/lm-cc init` ### Configuration Precedence 1. CLI options 2. Environment variables (`LMCC_API_KEY`, `LMCC_BACKEND`, `LMCC_MODEL`, `LMCC_BASE_URL`) 3. Config file (`lm-cc.yaml` or `.lm-cc.yaml`) 4. Built-in defaults ``` -------------------------------- ### Integrate LLM Clients for Entropy Calculation in PHP Source: https://context7.com/k-kinzal/lm-cc-php/llms.txt Details the implementation of LLM clients, specifically OllamaClient and OpenAiClient, for retrieving per-token entropy values in PHP. Ollama is recommended for local, free analysis, while OpenAI requires an API key and uses instruct models. Both clients adhere to the LlmClientInterface. ```php getTokenEntropies($code); foreach ($tokenEntropies as $te) { printf( "Token: '%s' | Entropy: %.4f | Offset: %d\n", $te->token, $te->entropy, $te->offset ); } // Example output: // Token: 'analyzeFile('/path/to/src/MyClass.php'); // Or analyze code directly $code = <<<'PHP' 0) { for ($i = 0; $i < $item; $i++) { $sum += $this->process($i); } } } return $sum; } } PHP; $result = $analyzer->analyze($code, 'Calculator.php'); // Access result properties echo "LM-CC Score: " . $result->score . "\n"; echo "Total Branching: " . $result->totalBranch . "\n"; echo "Total Compositional Level: " . $result->totalCompLevel . "\n"; echo "Max Depth: " . $result->maxCompLevel . "\n"; echo "Token Count: " . $result->tokenCount . "\n"; echo "Semantic Units: " . $result->unitCount . "\n"; echo "Tau Threshold: " . $result->tau . "\n"; // Check against threshold if ($result->exceedsThreshold(50.0)) { echo "Warning: Code complexity exceeds threshold!\n"; } // Convert to array for JSON serialization $data = $result->toArray(); // Returns: ['path' => '...', 'score' => 45.2, 'totalBranch' => 36.0, ...] ``` -------------------------------- ### Build Hierarchy Tree and Calculate LM-CC Score in PHP Source: https://context7.com/k-kinzal/lm-cc-php/llms.txt Demonstrates how to construct a semantic hierarchy tree from code units and calculate the LM-CC complexity score using branching factors and node depth. It requires the Lmcc\Hierarchy component and provides a manual implementation of the complexity formula. ```php build($units); $allNodes = $root->allNodes(); $alpha = 0.8; $totalBranch = 0; $totalDepth = 0; foreach ($allNodes as $node) { $totalBranch += $node->branchingFactor(); $totalDepth += $node->depth; } $lmccScore = $alpha * $totalBranch + (1 - $alpha) * $totalDepth; echo "LM-CC Score: " . $lmccScore . "\n"; ``` -------------------------------- ### Using Reusable GitHub Actions Workflow for LM-CC PHP Source: https://github.com/k-kinzal/lm-cc-php/blob/main/docs/ci-integration.md This snippet demonstrates how to use a pre-defined reusable workflow for LM-CC from the 'k-kinzal/lm-cc-php' repository. It specifies the threshold, paths for analysis, and allows passing secrets like an API key. ```yaml jobs: lm-cc: uses: k-kinzal/lm-cc-php/.github/workflows/lm-cc.yml@main with: threshold: '50.0' paths: 'src/' secrets: LMCC_API_KEY: ${{ secrets.LMCC_API_KEY }} ``` -------------------------------- ### LLM Client API Source: https://context7.com/k-kinzal/lm-cc-php/llms.txt Interfaces for interacting with LLM providers to retrieve per-token entropy values required for complexity analysis. ```APIDOC ## POST /llm/entropy ### Description Retrieves a list of token entropy values for a given code block using the configured LLM client. ### Method POST ### Parameters #### Request Body - **code** (string) - Required - The source code to analyze for token entropy. ### Response #### Success Response (200) - **token** (string) - The text of the token. - **entropy** (float) - The calculated entropy value. - **offset** (int) - The character offset in the source string. ### Response Example [ { "token": "