/**
* Magento Coding Standards Knowledge Base
* Central export for all coding standards knowledge.
*/
// Import for local use in functions below
import { INSECURE_FUNCTIONS } from './insecure-functions.js';
import { DISCOURAGED_FUNCTIONS } from './discouraged-functions.js';
import { RESTRICTED_CLASSES } from './restricted-classes.js';
import { XSS_ESCAPE_METHODS, TEMPLATE_ESCAPING_BEST_PRACTICES } from './xss-escape-methods.js';
// Re-export everything for external consumers
export {
INSECURE_FUNCTIONS,
isInsecureFunction,
getInsecureFunctionInfo,
type InsecureFunction,
} from './insecure-functions.js';
export {
DISCOURAGED_FUNCTIONS,
getDiscouragedFunctionReplacement,
getDiscouragedFunctionsByCategory,
type DiscouragedFunction,
} from './discouraged-functions.js';
export {
RESTRICTED_CLASSES,
getRestrictedClassReplacement,
isRestrictedClass,
getRestrictedClassesByCategory,
type RestrictedClass,
} from './restricted-classes.js';
export {
XSS_ESCAPE_METHODS,
NO_ESCAPE_ANNOTATION,
ALLOWED_FUNCTIONS_NO_ESCAPE,
HTML_METHOD_PATTERN,
getEscapeMethodForContext,
TEMPLATE_ESCAPING_BEST_PRACTICES,
type EscapeMethod,
type NoEscapeRule,
} from './xss-escape-methods.js';
export {
DEPRECATED_PATTERNS,
FORBIDDEN_PATTERNS,
PREFERRED_PATTERNS,
TEMPLATE_STRUCTURE_EXAMPLE,
VIEWMODEL_EXAMPLE,
LAYOUT_VIEWMODEL_EXAMPLE,
getDeprecatedPatterns,
getForbiddenPatterns,
checkForDeprecatedPatterns,
type TemplatePattern,
} from './template-patterns.js';
export {
JQUERY_DEPRECATIONS,
MAGENTO_JS_PATTERNS,
checkForJQueryDeprecations,
type JQueryDeprecation,
} from './jquery-deprecations.js';
export {
SEVERITY_LEVELS,
MAGENTO_RULES,
getRulesBySeverity,
getRulesByCategory,
getAllCategories,
getSeverityInfo,
type SeverityLevel,
type Rule,
} from './severity-rules.js';
/**
* Quick lookup for any pattern replacement
*/
export function getPatternReplacement(pattern: string): {
replacement: string | null;
source: string;
severity: number;
} | null {
// Check insecure functions first (highest severity)
if (pattern in INSECURE_FUNCTIONS) {
const info = INSECURE_FUNCTIONS[pattern];
return {
replacement: info.replacement,
source: 'insecure_function',
severity: 10
};
}
// Check discouraged functions
if (pattern in DISCOURAGED_FUNCTIONS) {
const info = DISCOURAGED_FUNCTIONS[pattern];
return {
replacement: info.replacement,
source: 'discouraged_function',
severity: 8
};
}
// Check restricted classes
if (pattern in RESTRICTED_CLASSES) {
const info = RESTRICTED_CLASSES[pattern];
return {
replacement: info.replacement,
source: 'restricted_class',
severity: 10
};
}
return null;
}
/**
* Get all knowledge for a specific task/operation
*/
export function getKnowledgeForTask(task: string): {
patterns: Array<{ name: string; use: string; example?: string }>;
avoid: Array<{ name: string; reason: string }>;
bestPractices: string[];
} {
const taskLower = task.toLowerCase();
const result = {
patterns: [] as Array<{ name: string; use: string; example?: string }>,
avoid: [] as Array<{ name: string; reason: string }>,
bestPractices: [] as string[],
};
// File operations
if (taskLower.includes('file') || taskLower.includes('read') || taskLower.includes('write')) {
result.patterns.push({
name: 'Magento\\Framework\\Filesystem\\DriverInterface',
use: 'All file system operations',
example: '$this->driver->fileGetContents($path)'
});
result.avoid.push({ name: 'file_get_contents', reason: 'Use DriverInterface instead' });
result.avoid.push({ name: 'fopen/fwrite/fclose', reason: 'Use DriverInterface instead' });
result.bestPractices.push('Inject DriverInterface via constructor');
result.bestPractices.push('Use Directory/File classes for path handling');
}
// Escaping
if (taskLower.includes('escape') || taskLower.includes('output') || taskLower.includes('html')) {
for (const [name, method] of Object.entries(XSS_ESCAPE_METHODS)) {
result.patterns.push({
name: `$escaper->${name}()`,
use: (method as any).use,
example: (method as any).example
});
}
result.avoid.push({ name: 'htmlspecialchars', reason: 'Use $escaper->escapeHtml()' });
result.bestPractices.push(...TEMPLATE_ESCAPING_BEST_PRACTICES);
}
// Validation
if (taskLower.includes('validate') || taskLower.includes('email') || taskLower.includes('input')) {
result.patterns.push({
name: 'Magento\\Framework\\Validator\\*',
use: 'Input validation',
example: 'new \\Magento\\Framework\\Validator\\EmailAddress()'
});
result.avoid.push({ name: 'Zend_Validate_*', reason: 'Use Magento or Laminas validators' });
}
// JSON
if (taskLower.includes('json') || taskLower.includes('serialize')) {
result.patterns.push({
name: 'Magento\\Framework\\Serialize\\Serializer\\Json',
use: 'JSON encoding/decoding',
example: '$this->jsonSerializer->serialize($data)'
});
result.avoid.push({ name: 'serialize/unserialize', reason: 'Object injection risk' });
result.avoid.push({ name: 'Zend_Json', reason: 'Deprecated, use Magento Json serializer' });
}
return result;
}