findings = $findings; } /** @return list */ public function ids(): array { return self::IDS; } /** * @param array $observation Minimized observation. * @return array */ public function analyze( string $check_id, array $observation, string $timestamp ): array { if ( ! in_array( $check_id, self::IDS, true ) ) { throw new InvalidArgumentException( 'rendered_analyzer_unavailable' ); } if ( 'available' !== ( $observation['capability']['state'] ?? null ) || ! is_array( $observation['page'] ?? null ) ) { return $this->not_tested( $check_id, $observation, $timestamp ); } switch ( $check_id ) { case 'frontend.dom_structure': return $this->dom_structure( $observation, $timestamp ); case 'frontend.responsive_layout': return $this->responsive_layout( $observation, $timestamp ); case 'frontend.render_stability': return $this->render_stability( $observation, $timestamp ); case 'frontend.assets': return $this->assets( $observation, $timestamp ); case 'frontend.javascript': return $this->javascript( $observation, $timestamp ); case 'frontend.third_parties': return $this->frontend_third_parties( $observation, $timestamp ); case 'frontend.forms': return $this->frontend_forms( $observation, $timestamp ); case 'frontend.interactions': return $this->interactions( $observation, $timestamp ); case 'accessibility.document_structure': return $this->accessible_structure( $observation, $timestamp ); case 'accessibility.alternatives': return $this->alternatives( $observation, $timestamp ); case 'accessibility.forms': return $this->accessible_forms( $observation, $timestamp ); case 'accessibility.keyboard_focus': return $this->keyboard_focus( $observation, $timestamp ); case 'accessibility.aria_controls': return $this->aria_controls( $observation, $timestamp ); case 'accessibility.contrast': return $this->contrast( $observation, $timestamp ); case 'accessibility.reflow_motion': return $this->reflow_motion( $observation, $timestamp ); case 'accessibility.media_tables': return $this->media_tables( $observation, $timestamp ); case 'privacy.forms_dataflow': return $this->forms_dataflow( $observation, $timestamp ); case 'privacy.notice_consent': return $this->notice_consent( $observation, $timestamp ); case 'privacy.cookies_storage': return $this->storage_visibility( $observation, $timestamp ); default: return $this->privacy_third_parties( $observation, $timestamp ); } } /** * @param array $observation Observation. * @return array */ private function dom_structure( array $observation, string $timestamp ): array { $dom = $this->section( $observation, 'dom' ); $issues = (int) $dom['duplicate_id_count'] + (int) $dom['empty_id_count'] + (int) $dom['heading_level_skips']; $status = $issues > 0 ? 'warning' : ( true === $dom['truncated'] ? 'not_tested' : 'pass' ); return $this->rendered( 'frontend.dom_structure', $status, $this->viewport_label( $observation ), $dom, true === $dom['truncated'] ? 0 : 1, 1, true === $dom['truncated'] ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function responsive_layout( array $observation, string $timestamp ): array { $viewports = $this->viewports( $observation ); $overflow = array_sum( array_column( $viewports, 'overflow_element_count' ) ); $clipped = array_sum( array_column( $viewports, 'clipped_fixed_count' ) ); $observed = count( $viewports ); $status = $overflow + $clipped > 0 ? 'fail' : 'not_tested'; return $this->rendered( 'frontend.responsive_layout', $status, $this->viewport_label( $observation ), array( 'viewports_observed' => $observed, 'overflow_elements' => $overflow, 'clipped_fixed' => $clipped, ), $observed, RenderedObservationContract::MAX_VIEWPORTS, 'partial', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function render_stability( array $observation, string $timestamp ): array { $runtime = $this->section( $observation, 'runtime' ); $status = (int) $runtime['late_shift_count'] > 0 ? 'warning' : 'info'; return $this->rendered( 'frontend.render_stability', $status, $this->viewport_label( $observation ), $runtime, 1, 2, 'partial', $observation, $timestamp, array( 'initial_layout_shift_not_observed' ) ); } /** * @param array $observation Observation. * @return array */ private function assets( array $observation, string $timestamp ): array { $resources = $this->section( $observation, 'resources' ); $status = true === $resources['truncated'] ? 'warning' : 'info'; $observed = (int) $resources['transfer_observed_count']; $total = max( $observed + 1, 1 + (int) $resources['script_count'] + (int) $resources['stylesheet_count'] + (int) $resources['image_count'] ); return $this->rendered( 'frontend.assets', $status, $this->viewport_label( $observation ), $this->without_refs( $resources ), $observed, $total, 'partial', $observation, $timestamp, array( 'rendering_resources_partially_blocked' ) ); } /** * @param array $observation Observation. * @return array */ private function javascript( array $observation, string $timestamp ): array { $resources = $this->section( $observation, 'resources' ); return $this->rendered( 'frontend.javascript', 'not_tested', $this->viewport_label( $observation ), array( 'script_elements_observed' => (int) $resources['script_count'], 'initial_error_capture' => 'unavailable', ), 0, 1, 'none', $observation, $timestamp, array( 'initial_console_errors_not_observed' ) ); } /** * @param array $observation Observation. * @return array */ private function frontend_third_parties( array $observation, string $timestamp ): array { $resources = $this->section( $observation, 'resources' ); $refs = $resources['external_origin_refs']; $status = array() === $refs ? ( true === $resources['truncated'] ? 'not_tested' : 'pass' ) : 'info'; return $this->rendered( 'frontend.third_parties', $status, $this->viewport_label( $observation ), array( 'external_origin_count' => count( $refs ), 'external_origin_refs' => $refs, 'resource_inventory_truncated' => $resources['truncated'], ), true === $resources['truncated'] ? 0 : 1, 1, true === $resources['truncated'] ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function frontend_forms( array $observation, string $timestamp ): array { $forms = $this->section( $observation, 'forms' ); $issues = (int) $forms['external_action_count'] + (int) $forms['get_sensitive_form_count']; $status = $issues > 0 ? 'warning' : ( true === $forms['truncated'] ? 'not_tested' : ( (int) $forms['form_count'] > 0 ? 'info' : 'pass' ) ); return $this->rendered( 'frontend.forms', $status, $this->viewport_label( $observation ), $this->without_refs( $forms ), (int) $forms['form_count'], max( 1, (int) $forms['form_count'] + ( true === $forms['truncated'] ? 1 : 0 ) ), true === $forms['truncated'] ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function interactions( array $observation, string $timestamp ): array { $interactions = $this->section( $observation, 'interactions' ); return $this->rendered( 'frontend.interactions', 'not_tested', $this->viewport_label( $observation ), $interactions, 0, max( 1, (int) $interactions['focusable_count'] ), 'none', $observation, $timestamp, array( 'interactions_not_activated' ) ); } /** * @param array $observation Observation. * @return array */ private function accessible_structure( array $observation, string $timestamp ): array { $dom = $this->section( $observation, 'dom' ); $headings = $dom['heading_counts']; $h1 = is_array( $headings ) ? (int) ( $headings[0] ?? 0 ) : 0; $issues = ( ! $dom['title_present'] ? 1 : 0 ) + ( ! $dom['lang_present'] ? 1 : 0 ) + ( 1 !== $h1 ? 1 : 0 ) + ( 1 !== (int) $dom['main_count'] ? 1 : 0 ) + (int) $dom['heading_level_skips'] + (int) $dom['duplicate_id_count']; $status = $issues > 0 ? 'fail' : ( true === $dom['truncated'] ? 'not_tested' : 'pass' ); return $this->rendered( 'accessibility.document_structure', $status, $this->viewport_label( $observation ), $dom, true === $dom['truncated'] ? 0 : 1, 1, true === $dom['truncated'] ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function alternatives( array $observation, string $timestamp ): array { $data = $this->section( $observation, 'alternatives' ); $eligible = (int) $data['image_count'] + (int) $data['svg_image_count']; $issues = (int) $data['missing_alt_count'] + (int) $data['unnamed_svg_image_count']; $status = $issues > 0 ? 'fail' : 'pass'; return $this->rendered( 'accessibility.alternatives', $status, $this->viewport_label( $observation ), $data, $eligible, max( 1, $eligible ), 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function accessible_forms( array $observation, string $timestamp ): array { $forms = $this->section( $observation, 'forms' ); $controls = (int) $forms['control_count']; $status = (int) $forms['unlabeled_control_count'] > 0 ? 'fail' : ( true === $forms['truncated'] ? 'not_tested' : 'pass' ); return $this->rendered( 'accessibility.forms', $status, $this->viewport_label( $observation ), $this->without_refs( $forms ), $controls, max( 1, $controls + ( true === $forms['truncated'] ? 1 : 0 ) ), true === $forms['truncated'] ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function keyboard_focus( array $observation, string $timestamp ): array { $data = $this->section( $observation, 'interactions' ); $issues = (int) $data['positive_tabindex_count'] + (int) $data['native_negative_tabindex_count']; $status = $issues > 0 ? 'warning' : 'not_tested'; return $this->rendered( 'accessibility.keyboard_focus', $status, $this->viewport_label( $observation ), $data, $issues > 0 ? 1 : 0, 2, 'partial', $observation, $timestamp, array( 'interactions_not_activated' ) ); } /** * @param array $observation Observation. * @return array */ private function aria_controls( array $observation, string $timestamp ): array { $data = $this->section( $observation, 'aria' ); $status = (int) $data['invalid_reference_count'] > 0 ? 'fail' : 'pass'; return $this->rendered( 'accessibility.aria_controls', $status, $this->viewport_label( $observation ), $data, (int) $data['aria_node_count'], max( 1, (int) $data['aria_node_count'] ), 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function contrast( array $observation, string $timestamp ): array { $data = $this->section( $observation, 'contrast' ); $observed = (int) $data['observed_count']; $eligible = (int) $data['eligible_count']; $status = (int) $data['failure_count'] > 0 ? 'fail' : 'not_tested'; $coverage = 0 === $observed ? 'none' : 'partial'; return $this->rendered( 'accessibility.contrast', $status, $this->viewport_label( $observation ), $data, $observed, max( 1, $eligible ), $coverage, $observation, $timestamp, array( 'simple_opaque_contrast_pairs_only' ) ); } /** * @param array $observation Observation. * @return array */ private function reflow_motion( array $observation, string $timestamp ): array { $viewports = $this->viewports( $observation ); $runtime = $this->section( $observation, 'runtime' ); $overflow = array_sum( array_column( $viewports, 'overflow_element_count' ) ); $clipped = array_sum( array_column( $viewports, 'clipped_fixed_count' ) ); $observed = count( $viewports ); $status = $overflow + $clipped > 0 ? 'fail' : 'not_tested'; return $this->rendered( 'accessibility.reflow_motion', $status, $this->viewport_label( $observation ), array( 'viewports_observed' => $observed, 'overflow_elements' => $overflow, 'clipped_fixed' => $clipped, 'animated_elements' => (int) $runtime['animated_element_count'], 'reduced_motion_alternate' => 'not_tested', ), $observed, RenderedObservationContract::MAX_VIEWPORTS + 1, 'partial', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function media_tables( array $observation, string $timestamp ): array { $data = $this->section( $observation, 'media_tables' ); $eligible = (int) $data['video_count'] + (int) $data['audio_count'] + (int) $data['table_count']; $issues = (int) $data['missing_caption_count'] + (int) $data['headerless_table_count']; $status = $issues > 0 ? 'fail' : ( $eligible > 0 ? 'info' : 'pass' ); return $this->rendered( 'accessibility.media_tables', $status, $this->viewport_label( $observation ), $data, $eligible, max( 1, $eligible ), 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function forms_dataflow( array $observation, string $timestamp ): array { $forms = $this->section( $observation, 'forms' ); $issues = (int) $forms['external_action_count'] + (int) $forms['get_sensitive_form_count']; $status = $issues > 0 ? 'warning' : ( true === $forms['truncated'] ? 'not_tested' : ( (int) $forms['form_count'] > 0 ? 'info' : 'pass' ) ); $fields = array( 'forms:' . (int) $forms['form_count'], 'controls:' . (int) $forms['control_count'], 'get_sensitive_forms:' . (int) $forms['get_sensitive_form_count'], ); return $this->dataflow( 'privacy.forms_dataflow', $status, array( 'rendered_forms' ), $fields, array( 'browser_dom_inventory' ), $forms['external_origin_refs'], (int) $forms['form_count'], max( 1, (int) $forms['form_count'] + ( true === $forms['truncated'] ? 1 : 0 ) ), true === $forms['truncated'] ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function notice_consent( array $observation, string $timestamp ): array { $privacy = $this->section( $observation, 'privacy' ); $count = (int) $privacy['notice_signal_count']; $status = $count > 0 ? 'info' : 'not_tested'; return $this->rendered( 'privacy.notice_consent', $status, $this->viewport_label( $observation ), array( 'privacy_path_signal_count' => $count, 'consent_behavior' => 'not_tested', ), $count > 0 ? 1 : 0, 2, 'partial', $observation, $timestamp, array( 'text_content_not_read', 'interactions_not_activated' ) ); } /** * @param array $observation Observation. * @return array */ private function storage_visibility( array $observation, string $timestamp ): array { return $this->rendered( 'privacy.cookies_storage', 'not_tested', $this->viewport_label( $observation ), array( 'storage_visibility' => 'deliberately_not_read' ), 0, 1, 'none', $observation, $timestamp, array( 'storage_values_not_read' ) ); } /** * @param array $observation Observation. * @return array */ private function privacy_third_parties( array $observation, string $timestamp ): array { $resources = $this->section( $observation, 'resources' ); $forms = $this->section( $observation, 'forms' ); $refs = array_values( array_unique( array_merge( $resources['external_origin_refs'], $forms['external_origin_refs'] ) ) ); $truncated = true === $resources['truncated'] || true === $forms['truncated']; $status = array() === $refs ? ( $truncated ? 'not_tested' : 'pass' ) : 'info'; return $this->dataflow( 'privacy.third_parties', $status, array( 'rendered_resources', 'rendered_forms' ), array(), array( 'browser_resource_inventory' ), $refs, $truncated ? 0 : 1, 1, $truncated ? 'partial' : 'complete', $observation, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function not_tested( string $check_id, array $observation, string $timestamp ): array { $reason = (string) ( $observation['capability']['reason'] ?? 'dom_unavailable' ); if ( in_array( $check_id, array( 'privacy.forms_dataflow', 'privacy.third_parties' ), true ) ) { return $this->findings->build( $check_id, 'not_tested', array( 'sources' => array(), 'processors' => array(), 'destinations' => array(), 'limitations' => array( 'browser_capability_' . $reason ), ), 0, 1, 'none', $timestamp ); } return $this->findings->build( $check_id, 'not_tested', array( 'viewport' => 'not_available', 'signals' => array( 'capability_state' => (string) ( $observation['capability']['state'] ?? 'blocked' ), 'reason' => $reason, ), 'limitations' => array( 'browser_capability_' . $reason ), ), 0, 1, 'none', $timestamp ); } /** * @param array $signals Safe aggregate signals. * @param array $observation Observation. * @param list $extra_limitations Fixed limitations. * @return array */ private function rendered( string $check_id, string $status, string $viewport, array $signals, int $observed, int $total, string $coverage, array $observation, string $timestamp, array $extra_limitations = array() ): array { return $this->findings->build( $check_id, $status, array( 'viewport' => $viewport, 'signals' => $signals, 'limitations' => $this->limitations( $observation, $extra_limitations ), ), $observed, $total, $coverage, $timestamp ); } /** * @param list $sources Sources. * @param list $fields Safe field classes. * @param list $processors Processors. * @param list $destinations Keyed destination refs. * @param array $observation Observation. * @return array */ private function dataflow( string $check_id, string $status, array $sources, array $fields, array $processors, array $destinations, int $observed, int $total, string $coverage, array $observation, string $timestamp ): array { return $this->findings->build( $check_id, $status, array( 'sources' => $sources, 'fields' => $fields, 'processors' => $processors, 'destinations' => $destinations, 'limitations' => $this->limitations( $observation ), ), $observed, $total, $coverage, $timestamp ); } /** * @param array $observation Observation. * @return array */ private function section( array $observation, string $section ): array { $value = $observation['page'][ $section ] ?? null; return is_array( $value ) ? $value : array(); } /** * @param array $observation Observation. * @return list> */ private function viewports( array $observation ): array { $value = $observation['page']['viewports'] ?? null; return is_array( $value ) ? array_values( $value ) : array(); } /** @param array $observation Observation. */ private function viewport_label( array $observation ): string { $widths = array_column( $this->viewports( $observation ), 'width' ); return 'homepage:' . implode( ',', array_map( 'strval', $widths ) ); } /** * @param array $observation Observation. * @param list $extra Extra limitations. * @return list */ private function limitations( array $observation, array $extra = array() ): array { $base = is_array( $observation['limitations'] ?? null ) ? $observation['limitations'] : array(); return array_values( array_unique( array_merge( $base, $extra ) ) ); } /** * @param array $value Map with local-only refs. * @return array */ private function without_refs( array $value ): array { unset( $value['external_origin_refs'] ); return $value; } }